
Create a complete Java program that allows the user to enter a positive integer, and which then determines and displays whether or not the number is prime. Prime numbers are positive integers greater than 1 which can only be evenly divided by 1 and themselves (e.g. 7 is prime, because it can only be evenly divided by 1 and 7, but 9 is not prime because it can be evenly divided by 3, in addition to 1 and 9).

import java.util.*;
class CheckPrime {
static boolean isPrime(long number) {
boolean isPrime = false;
int i = (int) Math.ceil(Math.sqrt(number));
while (i > 1) {
if ((number != i) && (number % i == 0)) {
isPrime = false;
break;
} else if (!isPrime)
isPrime = true;
--i;
}
return isPrime;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Number: ");
int num=input.nextInt();
if (isPrime(num)) {
System.out.println("No is Prime.");
}
else{
System.out.println("No is not Prime.");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.