-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumber.java
More file actions
49 lines (40 loc) · 1.38 KB
/
PrimeNumber.java
File metadata and controls
49 lines (40 loc) · 1.38 KB
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
43
44
45
46
47
48
49
// Implement a program that checks if a given number is prime.
import java.util.Scanner;
public class PrimeChecker {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
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.");
}
// Close the scanner
scanner.close();
}
// Method to check if a number is prime
public static boolean isPrime(int num) {
// Corner cases
if (num <= 1) {
return false; // Numbers less than or equal to 1 are not prime
}
if (num <= 3) {
return true; // 2 and 3 are prime numbers
}
if (num % 2 == 0 || num % 3 == 0) {
return false; // Eliminate multiples of 2 and 3
// Check for factors from 5 to sqrt(num)
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
}