Check Prime numbers program
Hi, I'm working on a program that checks if a user entered integer is prime or not.
It doesn't seem to work properly though. It thinks 25 is a prime number...and other ones that aren't or are prime is coming up wrong. Anyone have any ideas? Would be greatly appreciated.
public static void checkPrimeNumber()
Scanner sc = new Scanner(System.in);
System.out.print("Enter an Integer value: ");
int n = sc.nextInt();
int hF = (int) Math.sqrt(number);
int prime = 0;
for (int i = 2; i < hF; i++)
if (n % i == 0)
prime = 0;
prime = 1;
if (prime == 1 || number == 2)
System.out.println(n + " is a prime number");
System.out.println(n + " is not a prime number");
Re: Check Prime numbers program
1. In the for loop use i<=hF.
2. Instead of assigning prime=0 or prime=1 use prime++ for (hF%i==0 to be true).
no else part.
3. if prime>0 then not prime. Else prime.
I dint run these changes but I think this will work. :)