This content has been marked as final.
Show 11 replies

1. Re: Prime Number Program
807607 Nov 13, 2006 10:28 PM (in response to 807607)Hello Fellow Programmers
Simple way...have a for loop go from 2 untill the square root of that number and check and see if i % number = 0
Im having quite alot of trouble trying to solve this
problem, i was hoping you could help me. Anyway, Im
trying to make a program that when a user enters a
positive integer, the program will find out if it's
prime or not, im really stuck...any help is awesome!!
If i%0 never equals zero then it's prime.
If you do a search in here, this question has been asked before 
2. Re: Prime Number Program
807607 Nov 13, 2006 10:30 PM (in response to 807607)Sorry, i do not speak english very well, could you perhaps so me? 
3. Re: Prime Number Program
807607 Nov 13, 2006 10:34 PM (in response to 807607)As Norweed suggested:
1) Compute the square root of the number that was entered (check out Math.sqrt)
2) Loop from 2 to the square root you just calculated
3) If the number % the loop variable is equal to zero, the number isn't prime
4) If no numbers were found that result in number % the loop variable being equal to zero, the number is prime. 
4. Re: Prime Number Program
807607 Nov 13, 2006 10:37 PM (in response to 807607)quick example
say you input positive integer 9
then the method to check if it's prime..say
public boolean isPrime(int num) {
if (num == 1) return false; // 1 is not a prime #
if (num == 2) return true; // 2 is a prime #
// the rest you need to check via this for loop
for (int i = 2; i < num; i++) {
if (num % i == 0) return false;
}
return true;
} 
5. Re: Prime Number Program
807607 Nov 13, 2006 10:39 PM (in response to 807607)Thank you for your Help,
Do you mean something like this?
*************************************************
import javax.swing.JOptionPane;
public class PrimeNumber1 {
public static void main(String[] args){
String num = JOptionPane.showInputDialog("Enter a Number");
for(int i=2;i<=Math.sqrt(num);i++)
{
if ((num%0) != 0)
{
JOptionPane.showMessageDialog(null, "The Number is Prime!");
}
else
{
JOptionPane.showMessageDialog(null, "The Number is Not Prime!");
}
}
}
}
*************************************************
I still get a few errors...Can you help me one more time Please? 
6. Re: Prime Number Program
807607 Nov 13, 2006 10:40 PM (in response to 807607)I still get a few errors...Can you help me one more
When you post code, please wrap it in the [co[b]de] tags, it makes it readable for the rest of us.
time Please?
When you get compiler errors, include them in your post, indicating what lines they occur on, we don't know what line numbers your code has. 
7. Re: Prime Number Program
807607 Nov 13, 2006 10:56 PM (in response to 807607)First of all you mod with your variable i, you can't mod with 0.
Second of all, you can't say that if you mod it once and it's not 0, then it's automatically a prime number. you need to iterate through the whole set between 2 and sqrt(n), and if all fails to be == 0 when modding all the values, then, and only then is it a prime
so here's what it should look like:
Message was edited by:import javax.swing.JOptionPane; public class PrimeNumber1 { public static void main(String[] args) { String number = JOptionPane.showInputDialog("Enter a number"); int num = 0; try { num = Integer.parseInt(number); } catch (NumberFormatException nfe) { System.err.println(number + " is not a valid integer"); } if (num <= 2) { if (num == 1  num == 0) { JOptionPane.showMessageDialog(null, "The Number is NOT Prime!"); // i think this is generally true } else if (num == 2) { JOptionPane.showMessageDialog(null, "The Number is Prime!"); // i think this is generally true or i have it mixed up with the above. } else if (num < 0) { JOptionPane.showMessageDialog(null, "Not a valid input..."); } else { for (int i = 2; i <= Math.sqrt(num); i++) { if (num %2 == 0) { JOptionPane.showMessageDialog(null, "The Number is NOT Prime!"); } } JOptionPane.showMessageDialog(null, "The Number is Prime!"); } }
leggomyeggroll 
8. Re: Prime Number Program
807607 Nov 13, 2006 10:56 PM (in response to 807607)Thank you Lego,
when i run that, i get the following errors.
C:\Documents and Settings\Gregg Bell\Desktop\Java Assignment 3\PrimeNumberFinal.java:6: operator <= cannot be applied to java.lang.String,int
if (num <= 2) {
^
C:\Documents and Settings\Gregg Bell\Desktop\Java Assignment 3\PrimeNumberFinal.java:7: incomparable types: java.lang.String and int
if (num == 1) {
^
C:\Documents and Settings\Gregg Bell\Desktop\Java Assignment 3\PrimeNumberFinal.java:9: incomparable types: java.lang.String and int
} else if (num == 2) {
^
C:\Documents and Settings\Gregg Bell\Desktop\Java Assignment 3\PrimeNumberFinal.java:13: sqrt(double) in java.lang.Math cannot be applied to (java.lang.String)
for (int i = 2; i <= Math.sqrt(num); i++) {
^
C:\Documents and Settings\Gregg Bell\Desktop\Java Assignment 3\PrimeNumberFinal.java:14: operator % cannot be applied to java.lang.String,int
if (num %2 == 0) {
^
5 errors
Process completed. 
9. Re: Prime Number Program
807607 Nov 13, 2006 11:05 PM (in response to 807607)blah, couldnt edit my post a third time
Message was edited by:import javax.swing.JOptionPane; public class CheckPrime { public static void main(String[] args) { String number = JOptionPane.showInputDialog("Enter a number"); int num = Integer.parseInt(number); if (num <= 2) { if (num == 1  num == 0) { JOptionPane.showMessageDialog(null, "The Number is NOT Prime!"); // i think this is generally true } else if (num == 2) { JOptionPane.showMessageDialog(null, "The Number is Prime!"); // i think this is generally true or i have it mixed up with the above. } else if (num < 0) { JOptionPane.showMessageDialog(null, "Negative values are invalid!"); } else { for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { JOptionPane.showMessageDialog(null, "The Number is NOT Prime!"); return; } } JOptionPane.showMessageDialog(null, "The Number is Prime!"); } } }
leggomyeggroll
Message was edited by:
leggomyeggroll 
10. Re: Prime Number Program
807607 Nov 13, 2006 11:00 PM (in response to 807607)One issue your program will have is it will print a message everytime around the loop instead of a message once.
Write a separate method that works out if the number is prime and returns a boolean.if(number is prime) { print prime message; } else { print not prime message; }

11. Re: Prime Number Program
807607 Nov 13, 2006 11:07 PM (in response to 807607)Optionally you could do a try/catch on the num = Integer.parseInt(number) because it might not always be a valid input, i.e. the user might input 9.9
But i'll leave that up to you...