Java Program for Prime Number

 Hi Guys

Some interesting fact about Prime numbers 

😀

Two is the only even Prime number.

Every prime number can be represented in form of 6n+1 or 6n-1 except the prime number 2 and 3, where n is a natural number.

Two and Three are only two consecutive natural numbers that are prime.

Goldbach Conjecture: Every even integer greater than 2 can be expressed as the sum of two primes.


CODE: 😄


import java.util.*;

import java.lang.*;


class GFG {


// Check for number prime or not

static boolean isPrime(int n)

{


// Check if number is less than

// equal to 1

if (n <= 1)

return false;


// Check if number is 2

else if (n == 2)

return true;


// Check if n is a multiple of 2

else if (n % 2 == 0)

return false;


// If not, then just check the odds

for (int i = 3; i <= Math.sqrt(n); i += 2)

{

if (n % i == 0)

return false;

}

return true;

}


// Driver code

public static void main(String[] args)

{

if (isPrime(19))

System.out.println("true");


else

System.out.println("false");

}

}



Comments

Post a Comment