Java Program to Print Prime Numbers Between 1 and 100

Introduction

Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves. Printing prime numbers within a specific range is a common exercise in programming that helps you understand loops and conditional statements. This guide will walk you through writing a Java program that prints all prime numbers between 1 and 100.

Problem Statement

Create a Java program that:

  • Iterates through all numbers from 1 to 100.
  • Checks whether each number is a prime number.
  • Prints the prime numbers.

Expected Output:

  • Prime Numbers Between 1 and 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

Solution Steps

  1. Iterate Through Numbers from 1 to 100: Use a loop to go through all numbers in this range.
  2. Check if Each Number is Prime: Implement logic to determine whether a number is prime.
  3. Print Prime Numbers: Print the numbers that are determined to be prime.

Java Program

// Java Program to Print Prime Numbers Between 1 and 100
// Author: https://www.rameshfadatare.com/

public class PrimeNumbers1to100 {
    public static void main(String[] args) {
        // Step 1: Iterate through numbers from 1 to 100
        System.out.println("Prime numbers between 1 and 100 are:");
        for (int i = 2; i <= 100; i++) {
            // Step 2: Check if the current number is prime
            if (isPrime(i)) {
                // Step 3: Print the prime number
                System.out.print(i + " ");
            }
        }
    }

    // Method to check if a number is prime
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false;
        }
        // Only check up to the square root of the number
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Explanation

Step 1: Iterate Through Numbers from 1 to 100

  • A for loop is used to iterate through all integers between 2 and 100.

Step 2: Check if Each Number is Prime

  • The isPrime() method is used to determine if a number is prime:
    • If the number is less than or equal to 1, it is not prime.
    • The loop checks divisibility from 2 up to the square root of the number (Math.sqrt(number)), which is sufficient to determine primality.
    • If the number is divisible by any number within this range, it is not prime.

Step 3: Print Prime Numbers

  • If a number is determined to be prime, it is printed.

Output Example

Prime numbers between 1 and 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 

Conclusion

This Java program demonstrates how to print all prime numbers between 1 and 100. It covers essential concepts such as loops, conditional statements, and mathematical operations, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

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

Scroll to Top