java code to check is number is prime code example
Example 1: program to know if a number is prime
using namespace std;
bool Is_Prime(long long x){
if(x%2==0)return false;
for(int i=3;i*i<=x;i+=2)
if(x%i==0)return false;
return true;
}
int main(){
long long x;
cin>>x;
if(Is_Prime(x))cout<<"Is Prime";
else cout<<"Is not Prime";
}
Example 2: how to check if number prime in java
public class PrimeNumberInJava
{
public static void main(String[] args)
{
int a, b = 0, flag = 0;
int number = 66;
b = number / 2;
if(number == 0 || number == 1)
{
System.out.println(number + " is not prime number");
}
else
{
for(a = 2; a <= b; a++)
{
if(number % a == 0)
{
System.out.println(number + " is not prime number.");
flag = 1;
break;
}
}
if(flag == 0)
{
System.out.println(number + " is prime number.");
}
}
}
}