C Programming: Printing Prime Numbers from 1 to 20

In summary, the conversation discusses a programming problem where the goal is to print prime numbers from 1 to 20. The person asking for help has provided their code and is questioning why it is not working properly. They are also seeking clarification on the use of the continue statement and asking for help with fixing their code. The conversation also touches on the topic of whether 1 is considered a prime number or not.
  • #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:
Physics news on Phys.org
  • #2
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
 
  • #3
ok I've reset it, but its just prints 1 2 3
 
  • #4
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.
 
  • #5
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
 
  • #6
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?
 
  • #7
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.
 
  • #8
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.
 
  • #9
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.
 

What is C programming?

C programming is a high-level computer programming language that was developed in the 1970s. It is widely used for system and application software development and is known for its efficiency, portability, and flexibility.

What are prime numbers?

Prime numbers are positive integers that are only divisible by 1 and themselves. Examples include 2, 3, 5, 7, 11, and so on. Prime numbers play an important role in mathematics and have many real-world applications in fields such as cryptography and number theory.

How can I check if a number is prime in C programming?

To check if a number is prime in C programming, you can use a loop to divide the number by all numbers from 2 to the square root of the number. If the number is only divisible by 1 and itself, then it is a prime number. If the number is divisible by any other number, then it is not a prime number.

What is the most efficient way to find prime numbers in C programming?

The most efficient way to find prime numbers in C programming is by using the Sieve of Eratosthenes algorithm. This algorithm works by creating a list of numbers and crossing out all multiples of each prime number. The numbers that are left are the prime numbers.

Can I generate a list of prime numbers in C programming?

Yes, you can generate a list of prime numbers in C programming by using a loop to check if each number is prime and then adding it to a list if it is. You can also use the Sieve of Eratosthenes algorithm to generate a list of prime numbers efficiently.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
22
Views
765
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
39
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
887
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
Back
Top