C Programming: Printing Prime Numbers from 1 to 20

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 5K views
jam12
Messages
37
Reaction score
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:
Physics news on Phys.org
Well you never reset i in the outer loop. In other words, i=2 the first time the inner loop runs, then i=3 and x=4, then i=4 and x=5, etc
 
ok I've reset it, but its just prints 1 2 3
 
jam12 said:
ok I've reset it, but its just prints 1 2 3

Have you run through your code on paper? i.e. Make boxes labeled x, y, i and then walk through the code line by line? It will show you why y is not equaling 0.
 
ah yes, starting when x =4 and i=2 then 4%2=2 but this should be zero according to what i want.
Im not sure how to change this
 
Question:

You declare y as an int, then in your while loop you test if it's !=0. Can you do this in c? You have not assigned it a value. I guess, since it's an int, it is aujto-assigned zero?
 
DaveC426913 said:
Question:

You declare y as an int, then in your while loop you test if it's !=0. Can you do this in c? You have not assigned it a value. I guess, since it's an int, it is aujto-assigned zero?

yes you can do that in c, also it isn't auto assigned to 0, since if it was then, it would only print 1 and 2, but its printing 1 2 and 3.

The error is where y never becomes zero. like is said above i need to fix the condition for the while loop. eg: when x=4 and i=2 then 4%2=2 but this should be zero according to what i want.
Im not sure how to change this.
 
ok so i have:

#include<stdio.h>
main () {
int x, i=2, y=1;
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);
}
}
 
D H said:
Or, for that matter, you could execute this simple program by hand. Try it. Why doesn't your program print 5? (It only reports 1, 2, and 3 as primes.)

BTW, one is not prime. It stands by itself.

i know why it doesn't print 5 because y never becomes zero (zero remainder when when a number is a factor of another). Read the comments above before posting. I am not taking a stab in the dark, i know my problems as i have stated them and need help. If you want to post unhelpful comments then go elsewhere.

Im a mathematician, and it is a matter of opinion whether 1 is prime or not. Some textbooks say 1 is prime, whilst others don't, depending on how you define a prime number.

I don't want your help, ill just go elsewhere.