55. JAVA Program To check a Number is Prime number or Not.
How does this program work?
- In this program you are going to learn about to check if a number is Prime number or not using JAVA.
- Prime number means which is only divisible by 1 and that number itself, and cannot divisible by any other number.
- When the number is divided by 2, we use the remainder operator % to compute the remainder then flag will be effected.
- If the flag is zero, the number is prime number else the number is not a prime.
Here is the code
// Check Prime number or Not
import java.util.Scanner;public class Number
{
public static void main(String args[])
{
int num, i, count=0;
System.out.print("Enter a Number : \n");
Scanner obj = new Scanner(System.in );
num = obj.nextInt();
for(i=2; i< num; i++)
{
if(num%i == 0)
{
count++;
break;
}
}
if(count == 0)
{
System.out.print(num+" is a Prime Number");
}
else
{
System.out.print(num+" is not a Prime Number");
}
}
}