forked from StarlordHarsh/HacktoberfestDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isPrimeNum.java
42 lines (34 loc) · 856 Bytes
/
isPrimeNum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// check if a number is prime
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");
}
}