Java Program to Print an Alphabet Pyramid

Introduction

An alphabet pyramid is a pattern where each row contains letters of the alphabet in a specific pyramid-like arrangement. The letters can either be printed in sequence or repeated based on the row number. This guide will walk you through writing a Java program to print an alphabet pyramid of a given height.

Problem Statement

Create a Java program that:

  • Accepts the number of rows for the pyramid.
  • Prints an alphabet pyramid where each row contains alphabetic characters in pyramid form.

Example:

  • Input: 5

  • Output:

        A
       ABA
      ABCBA
     ABCDCBA
    ABCDEDCBA
    
  • Input: 4

  • Output:

       A
      ABA
     ABCBA
    ABCDCBA
    

Solution Steps

  1. Take Input: Accept the number of rows for the pyramid.
  2. Print the Alphabet Pyramid: Use nested loops to print spaces and alphabetic characters in a pyramid shape.
  3. Display the Result: Print the alphabet pyramid on the console.

Java Program

// Java Program to Print an Alphabet Pyramid
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class AlphabetPyramid {
    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: Print the alphabet pyramid pattern
        for (int i = 1; i <= rows; i++) {
            // Step 3: Print leading spaces for alignment
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Step 4: Print increasing alphabet sequence
            char ch = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            // Step 5: Print decreasing alphabet sequence
            ch -= 2;  // Move one step back to start the reverse pattern
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }

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

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

Explanation

Step 1: Take Input

  • The program prompts the user for the number of rows in the pyramid.

Step 2-3: Print Leading Spaces for Alignment

  • Before printing the alphabetic characters, spaces are printed to center-align the pyramid.

Step 4: Print Increasing Alphabet Sequence

  • The increasing sequence of characters from A up to the current row's letter is printed using a loop. The loop begins with the character 'A' and prints the next alphabet on each iteration.

Step 5: Print Decreasing Alphabet Sequence

  • After printing the increasing sequence, the program prints the decreasing sequence, starting from the second-to-last letter of the row (i.e., the letter before the peak of the pyramid) and going backward.

Output Example

For an input of 5, the program prints:

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

For an input of 4, the program prints:

   A
  ABA
 ABCBA
ABCDCBA

Example with Different Input

For an input of 3, the program outputs:

  A
 ABA
ABCBA

Conclusion

This Java program demonstrates how to print an alphabet pyramid pattern using nested loops. By carefully managing the placement of spaces and alphabetic characters, the program creates a well-structured pyramid shape. This exercise is great for practicing loop control, character manipulation, and pattern generation in Java.

Comments