Write a program in Java to accept a number and check whether it is a prime number or not. Finally, display the message accordingly.
Answers
Answered by
5
import java.util.Scanner;
public class Prime{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int n = sc.nextInt();
int c = 0; //For number of factors.
for(int i = 1 ; i<=n ; i++){
if ( n % i == 0)
c++;
}
if ( c ==2 ) //If it has 2 factors, 1 and itself.
System.out.println("The number is a prime number.");
else
System.out.println("The number is NOT a prime number.");
}
}
Answered by
0
Answer:
Write a program in Java to accept a
number, and check weather it is a prime number
or not. Finally, display the message
accordingly
Similar questions
Math,
1 month ago
Chemistry,
1 month ago
CBSE BOARD X,
1 month ago
Social Sciences,
3 months ago
Math,
3 months ago
Math,
10 months ago