-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckprime.java
37 lines (30 loc) · 918 Bytes
/
checkprime.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
import java.util.Scanner;
public class checkprime {
// Function to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Take input from the user
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is prime
boolean isPrime = isPrime(number);
// Print the result
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
}