C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Check whether a number is prime (loop)

Introduction

A Prime number is a number greater than 1 that has no divisors other than 1 and itself.
Examples:

  • 2, 3, 5, 7, 11, 13, 17 are prime numbers.
  • 4, 6, 8, 9, 10 are not prime numbers because they are divisible by numbers other than 1 and themselves.

In this program, we’ll use a for, while and do..while loop to check whether a number is prime or not by counting its divisors.

 

C Program: Check whether a number is prime (loop)

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num, i, count = 0;

 

    // Input from user

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num <= 1) {

        printf("%d is not a prime number.\n", num);

        return 0;

    }

 

    // Check for divisors

    for (i = 2; i <= num / 2; i++) {

        if (num % i == 0) {

            count = 1;

            break;

        }

    }

 

    // Display result

    if (count == 0)

        printf("%d is a prime number.\n", num);

    else

        printf("%d is not a prime number.\n", num);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 7
7 is a prime number.

OUTPUT 2 :
Enter a positive integer: 10
10 is not a prime number.

OUTPUT 3 :
Enter a positive integer: 1
1 is not a prime number.


Explanation

  1. The user inputs a number.
  2. If the number is ≤ 1, it’s not prime.
  3. Using a for loop, we check divisibility from 2 to num / 2.
    • If any number divides num evenly, it’s not prime.
  4. The loop stops early (break) when a divisor is found to optimize performance.
  5. Finally, the result is displayed.

 

C Program: Check whether a number is prime (loop)

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num, i = 2, flag = 0;

 

    // Input from user

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num <= 1) {

        printf("%d is not a prime number.\n", num);

        return 0;

    }

 

    // Check for divisors using while loop

    while (i <= num / 2) {

        if (num % i == 0) {

            flag = 1;

            break;

        }

        i++;

    }

 

    // Display result

    if (flag == 0)

        printf("%d is a prime number.\n", num);

    else

        printf("%d is not a prime number.\n", num);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 13
13 is a prime number.

OUTPUT 2 :
Enter a positive integer: 15
15 is not a prime number.

OUTPUT 3 :
Enter a positive integer: 1
1 is not a prime number.


Explanation

  1. User inputs a number num.
  2. If num <= 1, it’s not a prime.
  3. A variable i starts at 2.
  4. The loop checks divisibility (num % i == 0) for all i values up to num / 2.
  5. If any divisor is found, the flag is set and the loop breaks.
  6. Based on the flag value, the result is printed.

 

C Program: Check whether a number is prime (loop)

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num, i = 2, flag = 0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num <= 1) {

        printf("%d is not a prime number.\n", num);

        return 0;

    }

 

    // Check for divisors using do...while loop

    do {

        if (num % i == 0) {

            flag = 1;

            break;

        }

        i++;

    } while (i <= num / 2);

 

    // Result

    if (flag == 0)

        printf("%d is a prime number.\n", num);

    else

        printf("%d is not a prime number.\n", num);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 13
13 is a prime number.

OUTPUT 2 :
Enter a positive integer: 15
15 is not a prime number.

OUTPUT 3 :
Enter a positive integer: 1
1 is not a prime number.


Explanation

  1. User enters a positive number.
  2. The loop starts from i = 2 and checks divisibility up to num / 2.
  3. The do..while loop executes at least once, checking each divisor.
  4. If any divisor divides num completely, flag becomes 1 and the loop breaks.
  5. Finally, if no divisor is found (flag == 0), the number is prime.