Skip to content

Commit 7227539

Browse files
feat(programs): add prime number check (basic and sqrt optimization)
1 parent 388ea72 commit 7227539

3 files changed

Lines changed: 112 additions & 10 deletions

File tree

content/programs/contents.mdx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ description: A collection of basic Java programs categorized by topic.
88
1. [Print an Integer](/programs/print-an-integer)
99
2. [Add Two Integers](/programs/add-two-integers)
1010
3. [Check Even or Odd Number](/programs/check-even-or-odd)
11-
4. [Add Two Binary](/programs/java-program-to-add-two-binary-numbers)
12-
5. [Add Two Complex Numbers](/programs/java-program-to-add-two-complex-numbers)
13-
6. [Multiply Two Numbers](/programs/multiply-two-numbers)
14-
7. [Check Leap Year](/programs/java-program-to-check-Leap-year)
15-
8. [Calculate Simple interest](/programs/calculate-simple-interest)
16-
9. [Check Divisibility](/programs/java-program-to-check-divisbility)
17-
10. [Calculate Quotient and Reminder](/programs/find-quotient-and-reminder)
18-
11. [Calculate Power of a Number](/programs/calculate-power-of-a-number)
19-
12. [Calculate Compound Interest](/programs/calculate-compound-interest)
20-
13. [Calculate Factorial of a Number](/programs/factorial-in-java)
11+
4. [Check Prime Number](/programs/java-program-to-check-prime-number)
12+
5. [Add Two Binary](/programs/java-program-to-add-two-binary-numbers)
13+
6. [Add Two Complex Numbers](/programs/java-program-to-add-two-complex-numbers)
14+
7. [Multiply Two Numbers](/programs/multiply-two-numbers)
15+
8. [Check Leap Year](/programs/java-program-to-check-Leap-year)
16+
9. [Calculate Simple interest](/programs/calculate-simple-interest)
17+
10. [Check Divisibility](/programs/java-program-to-check-divisbility)
18+
11. [Calculate Quotient and Reminder](/programs/find-quotient-and-reminder)
19+
12. [Calculate Power of a Number](/programs/calculate-power-of-a-number)
20+
13. [Calculate Compound Interest](/programs/calculate-compound-interest)
21+
14. [Calculate Factorial of a Number](/programs/factorial-in-java)
22+
15. [Swap Two Numbers](/programs/swap-two-numbers)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.

content/programs/meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
"---Conditional Checks---",
1919
"check-even-or-odd",
20+
"java-program-to-check-prime-number",
2021
"java-program-to-check-divisibility",
2122
"java-program-to-check-Leap-year",
2223
"java-program-to-check-whether-input-character-is-vowel-or-consonant",

0 commit comments

Comments
 (0)