#include <stdio.h>
/* Write C Program to find all prime numbers from 1 to N using while and do while loop provided by the user.*/
/* C program to to find all prime numbers from 1 to N using while loop.*/
#include <stdio.h>
int main()
{
int n,i=1,j,cnt=0;
printf("\n Enter Any Number:");
scanf("%d",&n);
printf("\n Prime number between 1 to %d \n",n);
while(i<=n)
{
cnt=0;
j=1;
while(j<=i)
{
if(i%j==0)
{
cnt++;
}
j++;
}
if(cnt==2)
{
printf(" %d",i);
}
i++;
}
return 0;
}
Output:
Enter Any Number
15
Prime number between 1 to 15
3 5 7 11 13
Program Explanation:
1. In the program given above the variables n,i,j,cnt is declared as integer variable. the variable i=1 is assigned to value 1 and cnt variable is initialized to value 0 i.e. cnt=0.
variable n is declared as the integer, the value in n is used as the range to find prime number 1....N .
2.
printf("\n Enter Any Natural Number:");
scanf("%d",&n);
printf() function prompt the user "Enter Any number" then control jumps to the input function
scanf("%d",&n); and wait for the user input. User inputs/enters number 15 which is get stored(scaned)in variable n.
printf("\n Prime number between 1 to %d \n",n);--> display message Prime number between 1 to 15
3.1 control jumps to while loop and test the condition while(i<=n) i.e. while(1<=15) which results in true, statements inside body of loop cnt=0 j=1 is processed.
for better understanding let take value of i=5 i.e fifth interation of outer while loop.
3.1 control jumps to while loop and test the condition while(i<=n) i.e. while(5<=15) which results in true, statements inside body of loop cnt=0 j=1 is processed.
3.1.1 Control jumps to inner while and test the condition while (j<=i) i.e. while(1<=5) which results in true statements inside body of loop
if(i%j==0)
{
cnt++;
}
j++;
executes.
if condition i.e. if(5%1==0) which is true and body of if get executed and value of cnt is incremented by 1 i.e. cnt=1, control comes out of if after that executes j++ , value of j is incremented by 1 now j becomes j=2
3.1.2 Control jumps to inner while and test the condition while (j<=i) i.e. while(2<=5) which results in true statements inside body of loop
if(i%j==0)
{
cnt++;
}
j++;
executes.
if condition i.e. if(5%2==0) which is false and body of if does not get execute and value of cnt wont be incremented. control comes out of if after that executes j++ , value of j is incremented by 1 now j becomes j=3
3.1.3 Control jumps to inner while and test the condition while (j<=i) i.e. while(3<=5) which results in true statements inside body of loop
if(i%j==0)
{
cnt++;
}
j++;
executes.
if condition i.e. if(5%3==0) which is false and body of if does not get execute and value of cnt wont be incremented. control comes out of if after that executes j++ , value of j is incremented by 1 now j becomes j=4
3.1.4 Control jumps to inner while and test the condition while (j<=i) i.e. while(4<=5) which results in true statements inside body of loop
if(i%j==0)
{
cnt++;
}
j++;
executes.
if condition i.e. if(5%4==0) which is false and body of if does not get execute and value of cnt wont be incremented. control comes out of if after that executes j++ , value of j is incremented by 1 now j becomes j=5
3.1.5 Control jumps to inner while and test the condition while (j<=i) i.e. while(5<=5) which results in true statements inside body of loop
if(i%j==0)
{
cnt++;
}
j++;
executes.
if condition i.e. if(5%5==0) which is true and body of if get executed and value of cnt will be incremented by 1 (cnt++) now cnt becomes 2 i.e cnt=2. control comes out of if after that executes the statements
if(cnt==2)
{
printf(" %d",i);
}
Now The value of cnt is 2
The condition tested if(cnt==2) i.e. if(2==2) is true and printf(" %d",i ) inside body of if executes, which display number 5.
The process Repeates for all the number up to N.
4. finally control come out of outer while loop when the value of i greater than n. Finally program stop the execution.