C Programming: Printing Prime Numbers from 1 to 20

AI Thread Summary
The discussion revolves around a programming issue in C where the user is attempting to print prime numbers from 1 to 20 but is only getting numbers 1, 2, and 3. The main problem identified is that the variable 'y' is not initialized before being used in the while loop, leading to incorrect results. Participants suggest that the user should reset 'y' and properly set the condition for the while loop to check for prime numbers accurately. There is also a debate about whether 1 is considered a prime number, with differing opinions based on various mathematical definitions. The user expresses frustration with unhelpful comments and indicates a desire to seek assistance elsewhere.
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.
 
The error is in this statement: while(i<x && y!=0) { ... }

You haven't set y before entering the loop. If y is zero the loop will never be entered.

Learn to use a debugger.
 
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);
}
}
 
  • #10
And does that do what you want? (No.)

Don't just stab in the dark when programming (or solving problems, in general).

Do you have a debugger? Do you know how to use it?
 
  • #11
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.
 
  • #12
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.
 

Similar threads

Back
Top