Java Program to Print a Hollow Pyramid

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

A hollow pyramid is a pyramid-shaped structure in which only the boundary elements are printed, leaving the inner part empty. In this guide, we will walk through a Java program that prints a hollow pyramid of a given height using nested loops.

Problem Statement

Create a Java program that:

  • Accepts the number of rows (height) for the pyramid.
  • Prints a hollow pyramid using stars (*), with only the boundary elements visible.

Example:

  • Input: 5
  • Output:
        *
       * *
      *   *
     *     *
    *********
    

Solution Steps

  1. Take Input: Accept the number of rows (height) from the user.
  2. Print the Hollow Pyramid: Use nested loops to print spaces and stars such that only boundary elements of the pyramid are printed, with the first and last row fully filled.
  3. Display the Result: Print the pyramid on the console.

Java Program

// Java Program to Print a Hollow Pyramid
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class HollowPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Get the number of rows for the pyramid
        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        // Step 2: Loop through each row
        for (int i = 1; i <= rows; i++) {
            // Step 3: Print spaces for proper alignment
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Step 4: Print stars and hollow spaces
            for (int k = 1; k <= (2 * i - 1); k++) {
                if (k == 1 || k == (2 * i - 1) || i == rows) {
                    System.out.print("*");  // Print star at the boundary or last row
                } else {
                    System.out.print(" ");  // Print space for hollow part
                }
            }

            // Move to the next line after printing each row
            System.out.println();
        }

        // Close the scanner
        scanner.close();
    }
}

Explanation

Step 1: Take Input

  • The program asks the user for the number of rows (height) of the pyramid.

Step 2: Loop Through Each Row

  • A for loop iterates from 1 to rows (the number of rows), where each iteration corresponds to one row of the pyramid.

Step 3: Print Spaces for Alignment

  • For each row, a nested loop prints the appropriate number of spaces (rows - i) before printing stars. This ensures that the pyramid is centered.

Step 4: Print Stars and Hollow Spaces

  • A second nested loop prints the stars and spaces in each row. The stars are printed at the beginning (k == 1), the end (k == (2 * i - 1)), and for the last row (i == rows). Spaces are printed for all other positions in the row to create the hollow effect.

Output Example

For an input of 5, the program outputs:

    *
   * *
  *   *
 *     *
*********

Example with Different Input

For an input of 3, the program outputs:

  *
 * *
*****

Conclusion

This Java program demonstrates how to print a hollow pyramid using nested loops. By controlling the placement of spaces and stars, the program is able to create a hollow pyramid pattern. This exercise helps in understanding how to manipulate loops and print complex patterns in Java programming.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare