DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Dec 2004
Posts
3

Does anyone know how to create a program to print prime numbers?

Once compiled, the program can ask me to enter an integer, but once I enter the integer(Lets say.. 10), I need all the prime numbers under 10 to print. So... 1,2,3,5,7 should print.

Thanks.

2. Senior Member
Join Date
Feb 2004
Posts
541
To tell if a number is prime you need to loop through the numbers less than it and see if that number is divisable by it.

Obviously don't use the number 1 because all integers are divisible by one. Also, you only need to try the numbers up to the integer in question divided by 2.

eg if the user enters 10, you only need to test up to 5.

If the user enters 12, test up to 6.

To see if one number is divisble by another number use the modulus operator (%). This number works out the remainder, so if number A is divisible by number B the expression A%B will equal 0.

3. Registered User
Join Date
Dec 2004
Posts
3
I was able to create this, but I have a small problem. How can I make it ask for the amount once and than I press any key to close the box? Every time I enter an integer, it gives me the results and asks the question again.

import java.io.*;

public class ListPrimes {

public static void main(String [] argv){
InputStream iStream = System.in;
String bufStr;
try {
System.out.print("Please enter # to list primes up to> ");
try{
int stop = Integer.parseInt(bufStr);
for(int x=1;x<=stop;x+=2){
if(isPrime(x)){
System.out.print(x+" ");
}
}
System.out.print("\nPlease enter # to list primes up to> ");
}
catch(Exception e1){
System.out.println("("+bufStr+") caused an error: "+e1.toString());
}
}
} catch (Exception e) {
System.out.println("Error: "+e.toString());
}

}
public ListPrimes(){
}
public static boolean isPrime(long _p){
if (_p<2 || _p%2==0) return false;
long max=(long)(Math.sqrt(_p));
for (long i = 3; i <= max; i+=2) {
if (_p%i==0) return false;
}
return true;
}

}

4. Here is the solution, although I didn't quite get the problem:

Code:
```import java.io.*;

public class ListPrimes {

public static void main(String [] argv){
InputStream iStream = System.in;
String bufStr;
try {
System.out.print("Please enter # to list primes up to (zero to stop)> ");
try{
int stop = Integer.parseInt(bufStr);
if (stop==0) break;
for(int x=1;x<=stop;x+=2){
if(isPrime(x)){
System.out.print(x+" ");
}
}
System.out.print("\nPlease enter # to list primes up to (zero to stop)> ");
} catch(Exception e1){
System.out.println("("+bufStr+") caused an error: "+e1.getMessage());
}
}
} catch (Exception e) {
System.out.println("Error: "+e.toString());
}
}
public ListPrimes(){}
public static boolean isPrime(long _p){
if (_p<2 || _p%2==0) return false;
long max=(long)(Math.sqrt(_p));
for (long i = 3; i <= max; i+=2) {
if (_p%i==0) return false;
}
return true;
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise