Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Program to List Prime Numbers from 1 to 100 – Step‑by‑Step Guide

What is a Prime Number?

A prime number is a natural number greater than one that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, and so on.

Important: 0 and 1 are not prime, and 2 is the sole even prime number.

Java Program to Print Prime Numbers Between 1 and 100

Below is a clear, commented Java program that enumerates all prime numbers from 1 to 100:

public class PrimeNumbersDemo {

    public static void main(String[] args) {
        int maxCheck = 100;
        StringBuilder primes = new StringBuilder();

        for (int i = 2; i <= maxCheck; i++) {
            if (isPrime(i)) {
                primes.append(i).append(' ');
            }
        }

        System.out.println("Prime numbers from 1 to " + maxCheck + " are:");
        System.out.println(primes.toString());
    }

    private static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= number / 2; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Expected Output

Prime numbers from 1 to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Feel free to modify maxCheck to generate primes up to any limit.

Java

  1. Java Hello World: Your First Program
  2. Constructor Overloading in Java – Explained with Practical Code Examples
  3. Java Multithreading Explained: Concepts, Lifecycle, and Practical Code Examples
  4. Java Program to Determine If a Number Is Prime
  5. Generate Fibonacci Sequence in Java: For, While, and Recursive Examples
  6. Java Program to Identify Armstrong Numbers Using For Loop
  7. Java Palindrome Number Checker: Algorithms Using While and For Loops
  8. Master Bubble Sort in Java: Clear Example & Step‑by‑Step Walkthrough
  9. Insertion Sort Algorithm in Java: A Clear Example & Step‑by‑Step Guide
  10. Selection Sort in Java: Step‑by‑Step Example & Full Code