Hi there,
Can anyone please help me with this issue???
I need to write an application that will prompt the user for a number and determine whether or not it is prime.
Application should ask the user to enter a positive integer greater than 2 or a -1 to quit.
If it is prime, it will output a message saying "x is a prime number."
If the number is not prime it will output a message saying " x is not a prime number." It will then output the factors for that number. Ex: 18 is not a prime number. Its factors are: 1,2,3,6,9,18
I came up to the part to create the loop, but I don't know where to start. I want to have a loop running from 2 to the square root of x. I will assume that the number entered is prime and will divide it by mod 2, if it's divisible than it'll say the number is not prime.
Here is my code, it's not working though... Please advise me if my approach is incorrect. Many thanks in advance...
CODE
import java.util.Scanner;
public class PrimeNumbers {
public static void main (String[] args){
Scanner input = new Scanner( System.in );
int x;
int integer;
int integerCounter;
integerCounter = 0;
x= 0;
System.out.println ("Please enter a positive integer greater than 2 or a -1 to quit");
integer = input.nextInt();
while (integer != -1)
{
integerCounter = integerCounter + 1;
System.out.println ("Please enter a positive integer greater than 2 or a -1 to quit");
integer = input.nextInt();
}
for (int i = 2;Math.sqrt(i); i++){
if (x % mod(2) == 0)
System.out.println (integer +"is not a prime number");
else {
System.out.println (integer + "is a prime number"); }
}
}
}