Prime Number or Not using Java

Prime Number or Not using Java

prime number is a naturalnumber greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.

Example:

import java.util.Scanner;

public class PrimeNumber {


public static void main(String[] args) {

int i,ans=0;
System.out.println(" Enter any number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();

for(i=2;i<=n/2;i++)
{
ans=n%i;

}
if(ans==0)
{
System.out.println(" Not a prime Number");
}
else
{
System.out.println(" Is a prime number");
}

}

}

Output:

 Enter any number
11
 Is a prime number




1 comment: