Results 1 to 5 of 5
Thread: Trying to generate prime numbers
 01172013, 12:21 AM #1Member
 Join Date
 Jan 2013
 Posts
 7
 Rep Power
 0
Trying to generate prime numbers
I started learning java yesterday and I've already gotten into trouble;) I'm trying to make a simple program that generates all prime numbers below 10
My problem is that for some reason the code at the bottom keeps printing out the wrong numbers. If i run the program
it will give me the numbers: 3, 5 , 7 , 9
I'm not sure if this is correct but I think it gives me the 9 becauseJava Code:if(i%n != 0)
Question is how do write the program so that when i = x it checks every number from 2 to x if NONE of these numbers has a remainder that does != 0 it prints it out as a prime number?
So for example if we set i = 5 then it would look like this:
5%2 = 1
5%3 = 2
5%4 = 1
5%x != 0 print as prime
Java Code:while(i < 10) { for(int n = 2; n < i; n++) { if(i%n != 0) // is prime { System.out.println(i + " is a prime"); break; } if(i%n == 0) break; } i++; }
 01172013, 06:00 PM #2Member
 Join Date
 Jul 2012
 Posts
 55
 Rep Power
 0
Re: Trying to generate prime numbers
The issue you are having with the loop is that no matter what the loop will break after the first iteration. To fix this issue you will need to do something along the lines of
Java Code:boolean isPrime = true; while(i < 10) { isPrime = true; for(int n = 2; n < i; n++) { if(i%n == 0) { isPrime = false; break; } } if(isPrime) System.out.println(i + " is a prime"); } i++;
 01172013, 08:50 PM #3Member
 Join Date
 Jan 2013
 Posts
 7
 Rep Power
 0
Re: Trying to generate prime numbers
I tried to just copy paste this code into my program and it just keeps printing "2 is a prime"
EDIT: I rewrote the code but i tried with the true and false statements that you suggested, it works now ;)
thanks for help, code below if someones intressted.
Java Code:boolean IsPrime = true; while(x < 99) { IsPrime = true; for(Counter = 2;Counter < x;Counter++) { if(x%Counter == 0 && Counter > 1) { //not a prime IsPrime = false; } } if(IsPrime) { IsPrime = true; System.out.println(x + " is a prime"); } x++; }
Last edited by Sulfuri; 01172013 at 09:24 PM.
 01172013, 09:39 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,389
 Blog Entries
 7
 Rep Power
 25
Re: Trying to generate prime numbers
FYI: Counter i always larger than 1 (because you have said so in the header of your forloop) so you can scratch that test in you ifstatement.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 01172013, 11:38 PM #5Member
 Join Date
 Jul 2012
 Posts
 55
 Rep Power
 0
Re: Trying to generate prime numbers
That was my fault with it being an infinite loop. I left the i++ outside the brackets of the while loop. If you place the i++ inside the last brackets the code should work fine. ie.
Java Code:boolean isPrime = true; while(i < 10) { isPrime = true; for(int n = 2; n < i; n++) { if(i%n == 0) { isPrime = false; break; } } if(isPrime) System.out.println(i + " is a prime"); i++; }
Similar Threads

prime numbers
By Daniel93 in forum New To JavaReplies: 8Last Post: 01062013, 08:33 PM 
Generate prime numbers within fiboncacci series?
By Manish87 in forum New To JavaReplies: 9Last Post: 08082010, 04:07 AM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 11:09 AM
Bookmarks