Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Prime Numbers
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// A school method based Java program to
// check if a number is prime
import java.lang.*;
import java.util.*;

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");
}
}

// This code is contributed by Ronak Bhensdadia