Introduction
If you are starting your programming journey, one of the most common and important programs you will learn is the Java prime number program.
This program helps you understand:
- Loops
- Conditions
- Logic building
- Problem-solving skills
In this guide, you will learn multiple ways to check prime numbers in Java, along with examples, explanations, and interview tips.
What is a Prime Number?
A prime number is a number greater than 1 that is divisible only by:
- 1
- Itself
๐ Examples of Prime Numbers:
2, 3, 5, 7, 11, 13
๐ Non-Prime Numbers:
4, 6, 8, 9, 10
๐ Special Note:
1 is NOT a prime number
Logic to Check Prime Number in Java
To check whether a number is prime:
- Take a number (n)
- If n โค 1 โ Not prime
- Check divisibility from 2 to n/2
- If divisible โ Not prime
- If not divisible โ Prime
Java Program to Check Prime Number (Basic Method)
public class PrimeNumber {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
System.out.println(num + ” is a Prime Number”);
else
System.out.println(num + ” is not a Prime Number”);
}
}
Output
29 is a Prime Number
Optimized Java Prime Number Program (Using โn)
Instead of checking up to n/2, we can optimize the program using square root.
public class PrimeNumberOptimized {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
System.out.println(isPrime ? “Prime” : “Not Prime”);
}
}
๐ Java Program Using While Loop
public class PrimeWhile {
public static void main(String[] args) {
int num = 29, i = 2;
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
while (i <= num / 2) {
if (num % i == 0) {
isPrime = false;
break;
}
i++;
}
}
System.out.println(isPrime ? “Prime” : “Not Prime”);
}
}
๐ Java Program Using Recursion
public class PrimeRecursion {
static boolean isPrime(int num, int i) {
if (num <= 2)
return (num == 2);
if (num % i == 0)
return false;
if (i * i > num)
return true;
return isPrime(num, i + 1);
}
public static void main(String[] args) {
int num = 29;
if (isPrime(num, 2))
System.out.println(“Prime”);
else
System.out.println(“Not Prime”);
}
}
๐ข Java Program to Print Prime Numbers from 1 to 100
public class PrimeRange {
public static void main(String[] args) {
for (int num = 1; num <= 100; num++) {
int count = 0;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
count++;
break;
}
}
if (count == 0 && num > 1) {
System.out.print(num + ” “);
}
}
}
}
โฑ๏ธ Time Complexity
Understanding performance is important:
- Basic method โ O(n)
- Optimized method โ O(โn)
๐ This is why optimized code is preferred in interviews.
Real-Life Applications of Prime Numbers
Prime numbers are widely used in:
- ๐ Cryptography (data security)
- ๐ณ Banking systems
- ๐ Password encryption
- ๐ Cybersecurity
โ ๏ธ Common Mistakes Beginners Make
Avoid these mistakes:
โ Considering 1 as prime
โ Not checking numbers โค 1
โ Using inefficient loops
โ Forgetting break statement
๐งช Example Inputs & Outputs
| Input | Output |
|---|---|
| 7 | Prime |
| 10 | Not Prime |
| 1 | Not Prime |
| 13 | Prime |
๐ Java Interview Questions
- What is a prime number?
- Why is 1 not a prime number?
- What is the fastest way to check prime?
- Explain time complexity of your code
- Write a program to print primes from 1 to 100
๐ Related Java Programs
You should also learn:
- Java Fibonacci Program
- Java Palindrome Program
- Java Factorial Program
- Java Pattern Programs
๐ (Link these internally after you publish them)
โ FAQs
Q1: Is 1 a prime number?
No, 1 is not a prime number.
Q2: What is the smallest prime number?
2 is the smallest prime number.
Q3: Can negative numbers be prime?
No, prime numbers are always positive.
Q4: Why use โn in prime checking?
It reduces time complexity and improves performance.
Q5: Are prime numbers important in real life?
Yes, especially in encryption and cybersecurity.
Learn Java From Scratch (Course CTA)
Want to become a Java developer and start your IT career?
๐ In our training program, you will get:
- Complete Java (Core + Advanced)
- Real-time project training
- Interview preparation
- Resume building support
- Placement guidance
๐ฏ Perfect for:
- Students (any degree)
- Beginners with no coding background
- Career switchers
๐ Join our Java Course in Chennai
๐ Learn step-by-step with practical training
๐ฒ Contact us now / WhatsApp to get started