-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimeChecker.java
More file actions
35 lines (28 loc) · 874 Bytes
/
primeChecker.java
File metadata and controls
35 lines (28 loc) · 874 Bytes
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
import java.util.Scanner;
public class primeChecker {
public static void main(String[] args) {
//scanner object
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
// Function to check if a number is prime returns a boolean , true or false
private static boolean isPrime(int n) {
//n in the number entered by the user
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}