C Programming: Printing Prime Numbers from 1 to 20

Click For Summary

Discussion Overview

The discussion revolves around a programming task in C to calculate and print prime numbers from 1 to 20. Participants are examining the code provided by the original poster, addressing issues related to logic and variable initialization, as well as the definition of prime numbers.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster's code compiles but incorrectly prints all numbers from 1 to 20, prompting questions about the use of the continue statement and variable initialization.
  • Some participants suggest that the variable 'i' is not being reset properly in the outer loop, affecting the inner loop's execution.
  • There is a concern about the variable 'y' being uninitialized before it is used in a condition, leading to potential logical errors in the program.
  • One participant points out that the condition in the while loop is problematic because 'y' must be set before the loop is entered.
  • Another participant emphasizes the importance of debugging and understanding the flow of the program, suggesting that the original poster should run through the code manually.
  • A debate arises regarding the definition of prime numbers, with one participant asserting that 1 is not prime, while another argues that it is a matter of opinion based on different textbook definitions.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the definition of prime numbers, particularly the status of the number 1. There is no consensus on the correctness of the code or the best approach to resolve the programming issues presented.

Contextual Notes

There are unresolved issues regarding the initialization of variables and the logical flow of the program, particularly concerning the handling of the variable 'y' and the conditions for determining prime numbers.

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

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
9
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K