This program finds all prime numbers from 0 to user defined number. For that it ask a number before giving output.
Example:
printf("Enter the No.");
scanf("%d", &a);
for(i=2; i<a; i++) {
for(j=2; j<i; j++) {
z = i%j;
if(z == 0) {
j = a;
}
}
scanf("%d", &a);
for(i=2; i<a; i++) {
for(j=2; j<i; j++) {
z = i%j;
if(z == 0) {
j = a;
}
}
if(i == 2)
printf("%d\n", i);
if(z != 0)
printf("%d\n", i);
}
}
if(z != 0)
printf("%d\n", i);
}
}
Output
Input: 10
Output: 2, 3, 5 7
How it works
First it ask for the input and it runs first loop up to given number. In second loop it divides the previous loop number (i.e. "i") by current number (i.e. "j") and checks if has remainder. If it finds 0 as remainder then it immediately terminates the second loop and continues first one. But if seconds loop ends with no 0 output then it marks as prime number and gives at output.