Java Prime Number Program (With Code, Output & Explanation for Beginners)

Java prime number program with code example and output for beginners

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:

  1. Take a number (n)
  2. If n โ‰ค 1 โ†’ Not prime
  3. Check divisibility from 2 to n/2
  4. If divisible โ†’ Not prime
  5. 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

InputOutput
7Prime
10Not Prime
1Not Prime
13Prime

๐ŸŽ“ 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

Scroll to Top