please solve the problem by using Java A prime number is a number greater than o
ID: 3793823 • Letter: P
Question
please solve the problem by using Java
A prime number is a number greater than one that is evenly divisible only by itself and 1. For example, the number 5 is prime because it can be evenly divided only by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a program that asks the user to enter a number that is greater than or equal to 2. If they enter a number less than two print an error message (and exit the program using the return statement). Otherwise print a message that indicates whether or not the number is primesExplanation / Answer
PrimeNumber.java
public class PrimeNumber {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter a number greater than or equals to 2:");
int inputNUmber = in.nextInt();
if( inputNUmber < 2){
System.out.println("Input number must be greater than 2");
}
boolean isPrime = true;
//loop through the numbers one by one
for(int i=2; i<=inputNUmber/2; i++){
if(inputNUmber % i == 0){
isPrime = false;
}
}
if(isPrime){
System.out.println("Given number is a prime number");
}
else{
System.out.println("Given number is not a prime number");
}
}
}
Output:
Please Enter a number greater than or equals to 2:
45
Given number is not a prime number
Please Enter a number greater than or equals to 2:
37
Given number is a prime number
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.