Object:-Write a Program in Java to check whether number is prime or not.
class prime
{
public static void main(String...s)
{
int n,i;
System.out.println("Enter the Number:");
n=Integer.parseInt(s[0]);
for(i=2; i < n; ++i)
{
if(n%i==0)
break;
}
if(i==n)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
}
Output
Enter the Number:5
Number is prime
Explanation
A number is prime if it is divisible by 1 or itself. For example 2, 3, 5, etc are prime numbers while 4, 6, 8, etc are not prime.