Sample Input and Output:
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
friends.