- #1
jam12
- 38
- 0
Homework Statement
Hello, i want to calculate and print prime numbers from 1 to 20. I've provided my code below, and the program compiles but its just printing all numbers from 1 to 20, why?
also have i used the continue statement correctly, since if it is found that a number is not prime then the next iteration starts in the for loop (ie all stuff below the continue statement is discarded).
Thanks
The Attempt at a Solution
#include<stdio.h>
main () {
int x, i=2, y;
printf("1\n2\n");
for(x=3;x<21;x++) {
i=2;
while(i<x && y!=0) {
y=x%i;
i=i+1;
}
if(y==0) continue;
else printf("%d\n",x);
}
}
Last edited: