Java prime number Program: 4 Ways to Simplify Programming in Java, Easily

prime numbers in java program

Have you ever sat, staring at your computer, with your coffee in your hand, thinking- how am I supposed to write a prime number program in Java and not screw it up!

Well, I’ve been there. Sounds simple on paper, right?

No jargon overload.

What are Prime Numbers (Quick Refresher 🧮)

In general, a prime number is just a number that is greater than 1, and cannot be divided into two parts, other than the parts of 1 and the parts of itself.

Examples:

2, 3, 5, 7, 11, 13 ✅ (prime)

4, 6, 8, 9, 10 ❌ (not prime)

I’ll explain later.

Why Prime Numbers in Java Program

In case you think, Why should I even bother with prime numbers in Java program, here it is:

And thus, not to pass exams, it is about the development of your coding muscles 💪.

Prime Numbers in Java Program: The Basic Approach

public class PrimeCheck {
    public static void main(String[] args) {
        int num = 29; // you can try with any number
        boolean isPrime = true;

        if (num <= 1) {
            isPrime = false;
        } else {
            for (int i = 2; i < num; 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.");
    }
}

Optimized Prime Numbers in Java Program (Method 2)

public class PrimeCheckOptimized {
    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;
                }
            }
        }

        if (isPrime)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}

This version made me realize the difference between smart code and code that writes more lines but not the right lines.

Prime Numbers in Java Program Using Recursion (Method 3)

public class PrimeRecursion {
    static boolean isPrime(int num, int i) {
        if (num <= 2)
            return (num == 2) ? true : false;
        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(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}

Generate Prime Numbers in Java Program (Method 4)

That’s when loops save the day:

public class PrimeList {
    public static void main(String[] args) {
        int limit = 100;

        for (int num = 2; num <= limit; num++) {
            boolean isPrime = true;

            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }

            if (isPrime)
                System.out.print(num + " ");
        }
    }
}

Typical errors that I committed when writing the Prime Number in Java Program.

  • Losing the memory that 0 and 1 are not prime numbers.
  • And there are only even numbers that are not prime (except 2!).
  • Failure to deal with negative inputs.

Final Thoughts ❤️

It’s small, yet powerful.

Related Reads:

Previous Article

🎨 Hex Color Code Explained: The Complete Guide to Colors, Pickers & Best Practices (2025)

Next Article

What is ASCII Code? ASCII Code Table & Alphabets Explained (2025 Guide)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨