C Program To Find Prime Number or Not using While Loop


Lets write a C program to check whether user input number is prime number or not, using while loop.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
while loop in C programming
if else statement in C

In this video tutorial we’re illustrating 3 methods to find if the user entered number is prime number or not.

While loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2. So our c program starts checking for divisibility from number 2.

Video Tutorial: C Program To Check Whether a Given Number is Prime Number or Not, using While Loop


[youtube https://www.youtube.com/watch?v=hHeESOUH2as]

YouTube Link: https://www.youtube.com/watch?v=hHeESOUH2as [Watch the Video In Full Screen.]

Method 1 Source Code: Prime Number or Not

#include<stdio.h>

int main()
{
    int num, count = 2, flag = 1;

    printf("Enter a number\n");
    scanf("%d", &num);

    while(count < num)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is not prime number\n", num);

    return 0;
}

Output 1:
Enter a number
7
7 is prime number

Output 2:
Enter a number
10
10 is not prime number

Logic: Method 1

We accept the number from the user. In while loop condition we check if the user entered number is greater than the number present in variable count. We are not checking for greater than or equal to, because all the positive natural numbers are divisible by itself, so we skip checking divisibility of user entered number by itself.

Inside while loop we keep incrementing the value of count by one for each iteration and we check if num%count == 0. If that condition is true, then we store 0 in variable flag and break out of the loop. That indicates there is yet another number(other than 1 and the number itself) which perfectly divides the user entered number.

Outside the while loop we check if the value of flag is 1 or 0. If its 1, then the user entered number is prime number orelse its not a prime number.

Method 2 Source Code: Prime Number or Not: Divide By 2

#include<stdio.h>

int main()
{
    int num, count = 2, flag = 1, inum;

    printf("Enter a number\n");
    scanf("%d", &num);

    inum = num / 2;

    while(count <= inum)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is not prime number\n", num);

    return 0;
}

Output 1:
Enter a number
41
41 is prime number

Output 2:
Enter a number
15
15 is not prime number

Logic: Method 2

Please read the logic for method 1 above before proceeding.
In this method, we divide the user entered number by 2. This reduces the number of iterations of while loop.

If num = 50;
inum = num / 2;
inum = 50 / 2;
inum = 25;

So its enough if we iterate through the while loop 25(num/2) times to check if number 50 is divisible by any number from 2 to 25.

Method 3 Source Code: Prime Number or Not: square root Method

#include<stdio.h>
#include<math.h>

int main()
{
    int num, count = 2, flag = 1, inum;

    printf("Enter a number\n");
    scanf("%d", &num);

    inum = sqrt(num);

    while(count <= inum)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is not prime number\n", num);

    return 0;
}

Output 1:
Enter a number
50
50 is not prime number

Output 2:
Enter a number
53
53 is prime number

Logic: Method 3

Please read the logic for method 1 above before proceeding.
In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of while loop.

If num = 50;
inum = sqrt(num);
inum = sqrt(50);
inum = 7;

So its enough if we iterate through the while loop 7( sqrt(num) ) times to check if number 50 is divisible by any number from 2 to 7.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if and else because we only have 1 line of code after if and else – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

You can also watch video for C Program To Find Prime Number or Not using For Loop

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *