1

I have code here for the program to list all prime numbers from 3-100. My main problem is that the program only prints out three. I think for some reason it's leaving the loop or something. I put a break within the for statement for it to leave the inner for loop immediately once it finds that the number is not prime so that it can print it out. But, it doesn't seem to be working.

#include <iostream> #include <conio.h> #include <cmath> using namespace std; int main() { bool prime = true; for (int x = 3; x <= 100; x++) { for (int y = 2; y <= (x - 1); y++) { if ((x % y) == 0) prime = false; break; } if (prime == true) cout<<x<<endl; } getche(); return 0; } 
3
  • Don't you need { } after if((x % y) == 0 ) { prime=false; break; } ? also you need to set prime = true at first loop start. Commented Oct 12, 2012 at 22:06
  • 2
    Side note: style: you might want to indent your nested for loops, might make it easier to read your own code, and catch exactly this sort of programming error. Commented Oct 12, 2012 at 22:46
  • 1
    Just take and print: primes.utm.edu/lists/small/10000.txt printf("2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97\n"); (JK) Commented Oct 12, 2012 at 23:47

11 Answers 11

6

The problem is:

for (int y = 2; y <= (x - 1); y++) { if ((x % y) == 0) prime = false; break; } 

when it should be

for (int y = 2; y <= (x - 1); y++) { if ((x % y) == 0) { prime = false; break; } } 

You break every time with prime set to true unless the first condition is met.

You also have to reset prime to true on each iteration:

for (int x = 3; x <= 100; x++) { prime = true; 
Sign up to request clarification or add additional context in comments.

Comments

2

When you set prime to false to say that a single number isn't prime, you never set prime to true again.

Comments

2

On the first line inside the first for loop, put this:

prime = true;

Currently, you never reset your prime flag back to true, so when you get to 4, and set prime to false, it never "finds" a prime number after, since prime is always false.

Full code:

#include <iostream> #include <conio.h> #include <cmath> using namespace std; int main() { bool prime = true; for (int x = 3; x <= 100; x++) { prime = true; for (int y = 2; y <= (x - 1); y++) { if ((x % y) == 0) { prime = false; break; } } if (prime == true) cout<<x<<endl; } getche(); return 0; } 

3 Comments

Yikes, didn't know my answer was worthy of a downvote. I'd love to get some feedback as to what I could improve.
Cool, added his missing braces.
It's a classic. No {} on the (x % y) test. Mark down fixed, some of these guys are a bit harsh.
1

after the first loop initialize prime=true;. because once prime is false it is not getting true again.

for (int x = 3; x <= 100; x++) { prime=true; ///rest of the code } 

Comments

1

you never reset prime to be true. After you cout you need to do prime = true;

Comments

0

Try this.

for (int x = 3; x <= 100; x++) { for (int y = 2; y <= (x - 1); y++) { if ((x % y) == 0) break; if((x-1)==y) cout<<x<<endl; } } 

Comments

0

The inner loop will always call the break statement since the IF only executes the first line because it is not wrapped in parentheses. Also you need to reset the bool variable for each number (i.e. inside the first loop)

if ((x % y) == 0) { prime = false; break; // previously it was always breaking } 

Previously it was basically doing this:

if ((x % y) == 0) { prime = false; } break; // WRONG! 

Comments

0
int main() { for (int x = 3; x <= 100; x++) { bool prime = true; for (int y = 2; y <= (x - 1); y++) { if ((x % y) == 0) { prime = false; break; } } if (prime == true) { cout<<x<<endl; } } getche(); return 0; } 

You needed to set prime inside the loop, and you wanted to set it to false and break on the test in your inner loop.

Old hand's tip number 45623, even though you can skip the braces if the code block is one line, don't.

Comments

0

As every one say this you have 2 errors:

1) you never reset value of prime, so add a prime = true to start of outer for.
2) convert if ((x % y) == 0) prime = false; break; to if ((x % y) == 0) {prime = false; break;} otherwise inner for will be executed only once!!

Comments

0

For Efficiency try this Its java version but you can try the same logic for C.

 boolean prime; int iCount = 0; int jCount = 0; for(i = 2; i <= 100; i++) { iCount += 1; prime = true; int squared = (int) (Math.sqrt(i)); for(j = 2; j < squared; j++){ jCount += 1; if(i%j == 0) prime = false; } if(prime) System.out.print(i+" "); } 

Comments

0

Here is a code for printing primes between 3 to 100. Used a counter variable n not the general algorithm to find primes.

#include <iostream.h> #include <conio.h> int main() { int i; int j; clrscr(); int count=0; for(j=100; j>=3; j--) { for(int i=1; i<=j; i++) { if(j%i==0) count++; } if(count==2) cout<<"\n"<<j; count=0; } getch(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.