00:00

Java Program to Print the First 100 Prime Numbers

Last updated:

A prime number is a whole number greater than 1 that is divisible only by 1 and itself, such as 2, 3, 5, 7 and 11. This page prints the first 100 prime numbers, the primes between 1 and 100, and shows a faster method for printing many primes at once.

1. Print the First 100 Prime Numbers

The program tests numbers one by one, starting from 2, and counts the primes it finds until it has 100. The isPrime() method only checks divisors up to the square root of the number (i * i <= n), which is enough to decide whether it is prime.

First100Primes.java
public class First100Primes {

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

    public static void main(String[] args) {
        int count = 0;
        int number = 2;

        System.out.println("First 100 prime numbers:");
        while (count < 100) {
            if (isPrime(number)) {
                System.out.printf("%4d", number);
                count++;
                if (count % 10 == 0) {
                    System.out.println();   // 10 primes per line
                }
            }
            number++;
        }
    }
}
Output
First 100 prime numbers:
   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 101 103 107 109 113
 127 131 137 139 149 151 157 163 167 173
 179 181 191 193 197 199 211 223 227 229
 233 239 241 251 257 263 269 271 277 281
 283 293 307 311 313 317 331 337 347 349
 353 359 367 373 379 383 389 397 401 409
 419 421 431 433 439 443 449 457 461 463
 467 479 487 491 499 503 509 521 523 541

2. Print Prime Numbers Between 1 and 100

Here the loop runs over a fixed range instead of counting primes. There are 25 prime numbers between 1 and 100.

PrimesUpTo100.java
public class PrimesUpTo100 {
    public static void main(String[] args) {
        int found = 0;

        System.out.println("Prime numbers from 1 to 100:");
        for (int n = 2; n <= 100; n++) {
            boolean prime = true;
            for (int i = 2; i * i <= n; i++) {
                if (n % i == 0) {
                    prime = false;
                    break;
                }
            }
            if (prime) {
                System.out.print(n + " ");
                found++;
            }
        }
        System.out.println();
        System.out.println("Total: " + found);
    }
}
Output
Prime numbers from 1 to 100:
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
Total: 25

3. Print Primes Using the Sieve of Eratosthenes

The Sieve of Eratosthenes is a fast way to find all primes up to a limit. It starts by assuming every number is prime, then, for each prime p, crosses out all multiples of p. Whatever is left uncrossed is prime.

SieveOfEratosthenes.java
public class SieveOfEratosthenes {
    public static void main(String[] args) {
        int limit = 150;
        boolean[] composite = new boolean[limit + 1];   // false means "still prime"

        for (int p = 2; p * p <= limit; p++) {
            if (!composite[p]) {
                for (int multiple = p * p; multiple <= limit; multiple += p) {
                    composite[multiple] = true;
                }
            }
        }

        System.out.println("Primes up to " + limit + ":");
        for (int n = 2; n <= limit; n++) {
            if (!composite[n]) {
                System.out.print(n + " ");
            }
        }
        System.out.println();
    }
}
Output
Primes up to 150:
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 101 103 107 109 113 127 131 137 139 149

4. Summary

  • To print the first 100 primes, test numbers from 2 upward and count the primes until you reach 100.
  • Checking divisors only up to the square root (i * i <= n) is enough and much faster.
  • There are 25 primes between 1 and 100, and the 100th prime is 541.
  • The Sieve of Eratosthenes finds all primes up to a limit very quickly.

Related Programs