program for prime number in c using while loop

C Program To check a number is prime number or not using while loop.










Flowchart











Algorithm

1.start

2.take Input and store in n

3. Intialize x with 2 (x=2)

4.while n%x==0  goto step 6

5.Increment the x value

6.if x==n then print " Entered number is prime"

else  print " Entered number is not prime"

7.Stop

 Code:


#include <stdio.h>

int main() {
    int n,x=2;
    printf("Enter a Number:");
    scanf("%d",&n);
    while(n%x!=0)
    {
        x=x+1;
    }
    if(x==n)
    {
        printf("%d is Prime Number",n);
    }
    else
    {
        printf("%d is Not a Prime Number",n);
    }
    return 0;
}

Sample Input and Output:

Enter a Number:3
3 is Prime Number

 Explanation of above program 

In above program,we are calculating the prime number using while loop.So Let's start!

what is Prime Number ?

Prime Number is any number that is only divisible by itself and 1.Example of  Prime number is a follow

2, 3, 5, 7, 11, 13, 17, 19 ...

First let's understand why above number are prime,from the definition of prime number we get to known that A number will be prime if it is divisible by 1 and itself,so we known that 2 is divisible only with 1 and itself,Therefore it is prime number. Similarly for remaining number(3,5,7,11,13,17,19) as well.

Let's understand code line by line

In the first line of code,we are importing the header file as usual to use the function associated with that header file i.e. stdio.h(Standard Input Output header file).In the next line,we are defining main() with return type int which mean we are suppose to return integer value from this function.those who don't known the actual use of main function please refer to this article

from the next line of code our actual program will start because before that we used to write the code which are necessary to run any c program and it's common code for almost all program.

In the next line we have declared two variable n and x of type int(Integer DataType).those who follow my blog regularly  might known   what is int here please click here to learn more about int and other data type.here one more important think to notice is we have done   x=2 means now x is assigned with value 2.Stay Tuned to get to known why we have assigned 2 and not any other number.

After that we are printing the statement "Enter a Number" so that user can get to known that we have to put value so that our program can find whether entered number is prime or not,to take input we have simply scanf() function with %d format specifier because we are storing user input in n and it is of integer data type

after that main core of our program is there i.e. while loop with some condition which help us to find entered number is prime or not,The condition with while loop is as follow 

while n % x ! = 0:

the meaning of this condition is divide n with x and check the remainder of it whether it is equal to 0 or not if not then execute the code inside the while loop.

Let's understand while loop execution iteration by iteration

Assume n=5

1st iteration 

the  value of x is 2 

and the n is 5

while (5 % 2 ! = 0) ( Since n % x!=0)

Meaning of Above condition is do module of 5 with 2 and  check if is not equal to 0 and then execute the code which is inside while loop(here ! is used as not operator).if modulo of n % x = = 0 then don't execute the code inside the while loop 

5 % 2 =1   - > 1


In above computation we got remainder as 1 Therefore Above condition  will satisfies and cursor will go inside while loop and inside the loop there is only one statement i.e. we are incrementing the value of x.so after incrementing the value of x.So now the value of x is 3 the program will goto the  next iteration.

2 iteration 

Now the value of x is 3

5 % 3 = = 0  - > 1

1 ! = 0

Now When we do 5 % 3 then it will again remainder as 1 therefore  the control will go inside and the value of x will be incremented with 1.So now the value of x is 4.

3 iteration 

Now the value of x is 4

5 % 4 = = 0  - > 1

1 ! = 0

Now when we do 5 % 4 then the remainder would be 1 so still the while loop condition satisfies as we known that the 1 is not equal to 0 therefore again x will be incremented 

4 iteration 

Now the value of x is 5

5 % 5 = = 0 -> 0

0 = =0

Now when we do 5 % 5 then the remainder would be 0 so the while loop condition is not getting satisfied therefore the execution 


If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

  Previous Next

Post a Comment

Previous Post Next Post