|
| 1 | +--- |
| 2 | +title: Java Program to Check Prime Number |
| 3 | +shortTitle: Check Prime Number |
| 4 | +description: Learn how to check whether a number is prime in Java using simple loop-based methods and an optimized square-root approach. |
| 5 | +--- |
| 6 | + |
| 7 | +A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. In this article, we’ll implement two approaches to check if a number is prime: |
| 8 | + |
| 9 | +- Basic divisor counting (loop up to n) |
| 10 | +- Optimized method (loop up to √n) |
| 11 | + |
| 12 | +Before you start, you may want to review: |
| 13 | + |
| 14 | +- [Java Variables and Literals](/docs/variables-and-literals) |
| 15 | +- [Java Operators](/docs/operators) |
| 16 | +- [Java Basic Input and Output](/docs/basic-input-output) |
| 17 | + |
| 18 | +## 1) Basic Approach (Loop up to n) |
| 19 | + |
| 20 | +This method is straightforward but less efficient for large n. |
| 21 | + |
| 22 | +### Example |
| 23 | + |
| 24 | +```java |
| 25 | +class Main { |
| 26 | + public static void main(String[] args) { |
| 27 | + int n = 29; // change value to test |
| 28 | + |
| 29 | + if (isPrime(n)) { |
| 30 | + System.out.println(n + " is a prime number."); |
| 31 | + } else { |
| 32 | + System.out.println(n + " is not a prime number."); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static boolean isPrime(int n) { |
| 37 | + if (n <= 1) return false; // 0, 1, and negatives are not prime |
| 38 | + int count = 0; |
| 39 | + for (int i = 1; i <= n; i++) { |
| 40 | + if (n % i == 0) count++; |
| 41 | + if (count > 2) return false; // early exit if more than 2 divisors |
| 42 | + } |
| 43 | + return count == 2; // exactly two divisors: 1 and n |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +#### Output (for n = 29) |
| 49 | + |
| 50 | +```plaintext |
| 51 | +29 is a prime number. |
| 52 | +``` |
| 53 | + |
| 54 | +## 2) Optimized Approach (Loop up to √n) |
| 55 | + |
| 56 | +You only need to check divisors up to the square root of n. |
| 57 | + |
| 58 | +### Example |
| 59 | + |
| 60 | +```java |
| 61 | +class Main { |
| 62 | + public static void main(String[] args) { |
| 63 | + int n = 30; // change value to test |
| 64 | + |
| 65 | + if (isPrimeSqrt(n)) { |
| 66 | + System.out.println(n + " is a prime number."); |
| 67 | + } else { |
| 68 | + System.out.println(n + " is not a prime number."); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static boolean isPrimeSqrt(int n) { |
| 73 | + if (n <= 1) return false; |
| 74 | + if (n == 2) return true; |
| 75 | + if (n % 2 == 0) return false; // even numbers > 2 are not prime |
| 76 | + |
| 77 | + for (int i = 3; i * i <= n; i += 2) { |
| 78 | + if (n % i == 0) return false; |
| 79 | + } |
| 80 | + return true; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### Output (for n = 30) |
| 86 | + |
| 87 | +```plaintext |
| 88 | +30 is not a prime number. |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### Notes & Tips |
| 94 | + |
| 95 | +- Time complexity: |
| 96 | + - Basic method: O(n) |
| 97 | + - Optimized method: O(√n) |
| 98 | +- Edge cases: n ≤ 1 (not prime), n = 2 (prime), even numbers > 2 (not prime) |
| 99 | +- For large ranges of numbers, consider the Sieve of Eratosthenes. |
0 commit comments