(adsbygoogle = window.adsbygoogle || []).push({}); 1. The problem statement, all variables and given/known data

Hello, i want to calculate and print prime numbers from 1 to 20. Ive 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

3. 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);

}

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: C programming prime numbers

**Physics Forums | Science Articles, Homework Help, Discussion**