Computer Science, asked by seeratjahan2704, 3 months ago

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 PARCH0
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 harjitbadwal036
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