C++ Program to find Prime Number or Not using While Loop

Before going to the program for Prime Number or Not first let us understand what is a Prime Number?

Prime Number:

                 A Prime Number is a number greater than 1 and which is only divisible by 1 and the number itself.

For example,

                17 is a Prime Number, because 17 is not divisible by any number other than 1 and 17.

To find whether a Number is Prime Number or Not it is enough to check whether ‘n’ is divisible by any number between 2 and √n. If it is divisible then ‘n’ is not a Prime Number otherwise it is a Prime Number.

Related: C Program to display Prime Numbers between Two Intervals

Program code for Prime Number or Not in C++:


/* Prime Number or Not */
#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
    int n, i=2, flag=0;
    clrscr();
    
    cout<<"Enter a positive integer value: ";
    cin>>n;
    
    /* Loop to check whether 'n' is divisible by any number between 2 and sqrt(n) */
    while(i<=sqrt(n))
    {
        if(n%i==0)
        {
            flag=1;
            break;
        }
        i++;
    }
    
    /* if else condition to print Prime Number or Not */
    if (flag==0)
        cout<<n<<" is a prime number.";
    else
        cout<<n<<" is not a prime number.";
    getch();
}

Related: Prime number or Not in C using For Loop

Working:

  • First the computer reads the positive integer value from the user.
  • Then using while loop it checks whether ‘n’ is divisible by any number between 2 and √n.
  • Finally if else condition is used to print the number is prime number or not .

Step by Step working of the above Program Code:

For Prime Number:

  1. Let us assume that a user enters the positive integer value as 17.
  2. It assigns the value of flag=0, n=17.
  3. It assigns the value of i=2 and the loop continues till the condition of the while loop is true.

3.1.   i<=sqrt(n)    (2<=√17)    while loop condition is true

n%i==0    (17%2==0)    if condition is false

i++            (i=i+1)     So  i=3

3.2.   i<=sqrt(n)    (3<=√17)    while loop condition is true

n%i==0    (17%3==0)    if condition is false

i++            (i=i+1)     So  i=4

3.3.   i<=sqrt(n)    (4<=√17)    while loop condition is true

n%i==0    (17%4==0)    if condition is false

i++            (i=i+1)     So  i=5

3.4.   i<=sqrt(n)    (5<=√17)    while loop condition is false

It comes out of the while loop.

  1. flag==0    (0==0)    if condition is true

So it prints 17 is a prime number.

  1. Thus program execution is completed.

For Not a Prime Number:

  1. Let us assume that a user enters the positive integer value as 21.
  2. It assigns the value of flag=0, n=21.
  3. It assigns the value of i=2 and the loop continues till the condition of the while loop is true.

3.1.   i<=sqrt(n)    (2<=√21)    while loop condition is true

n%i==0    (21%2==0)    if condition is false

i++            (i=i+1)     So  i=3

3.2.   i<=sqrt(n)    (3<=√21)    while loop condition is true

n%i==0    (21%3==0)    if condition is true

It assigns flag=1

breaks the loop and comes out of the while loop

  1. flag==0    (1==0)    if condition is false

So it goes to else part and prints 21 is not a prime number.

  1. Thus program execution is completed.

Output:

prime number or not

prime number or not

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

2 Responses

  1. Khushboo jain says:

    What if you enter 2,how the loop works?

    • devang says:

      1. If you enter 2.

      2. It assigns the value of flag=0, n=2.

      3. It assigns the value of i=2 and the loop continues till the condition of the while loop is true.

      3.1. i<=sqrt(n) (2<=√2) while loop condition is false.

      4. flag==0 (0==0) if condition is true
      So it prints 2 is a prime number.

Leave a Reply

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