Skip to content

Commit 3819941

Browse files
Add PrimeCheck algorithm in Java
1 parent bb6385e commit 3819941

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

add-primecheck-algorithm

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.thealgorithms.math;
2+
3+
public class PrimeCheck {
4+
public static boolean isPrime(int n) {
5+
if (n <= 1) return false;
6+
for (int i = 2; i <= Math.sqrt(n); i++) {
7+
if (n % i == 0) return false;
8+
}
9+
return true;
10+
}
11+
12+
public static void main(String[] args) {
13+
int num = 7;
14+
if (isPrime(num))
15+
System.out.println(num + " is a prime number.");
16+
else
17+
System.out.println(num + " is not a prime number.");
18+
}
19+
}

0 commit comments

Comments
 (0)