Prime Number Program
Hello Fellow Programmers
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!!
Whoops, i forgot to mention that i am trying to use a FOR Loop.
Message was edited by:
Branch83wr
> Hello Fellow Programmers
>
> 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!!
Simple way...have a for loop go from 2 untill the square root of that number and check and see if i % number = 0
If i%0 never equals zero then it's prime.
If you do a search in here, this question has been asked before
Sorry, i do not speak english very well, could you perhaps so me?
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.
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;
}
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?
> I still get a few errors...Can you help me one more
> time Please?
When you post code, please wrap it in the [code][/code] tags, it makes it readable for the rest of us.
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.
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:
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!");
}
}
Message was edited by:
leggomyeggroll
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.
blah, couldnt edit my post a third time
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!");
}
}
}
Message was edited by:
leggomyeggroll
Message was edited by:
leggomyeggroll
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;
}
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...