Java Program to Print Character Pyramid Pattern

Introduction

Printing a character pyramid pattern is a common exercise to practice loops and formatting in Java. Instead of using stars (*), this program will print characters like A, B, C, etc., in a pyramid pattern.

Problem Statement

Create a Java program that:

  • Accepts the number of rows (size) for the pyramid.
  • Prints a pyramid using alphabetic characters (A, B, C, etc.).

Example:

  • Input: size = 5
  • Output:
        A
       A B
      A B C
     A B C D
    A B C D E
    

Solution Steps

  1. Input the Size of the Pyramid: The size determines the number of rows in the pyramid.
  2. Use Nested Loops: The outer loop will control the number of rows, while the inner loops will handle printing spaces and characters.
  3. Display the Character Pyramid: Print alphabetic characters, incrementing from A to the character corresponding to the row number.

Java Program

// Java Program to Print a Character Pyramid Pattern
// Author: [Your Name]

import java.util.Scanner;

public class CharacterPyramid {
    public static void main(String[] args) {
        // Step 1: Accept the size of the pyramid
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the pyramid: ");
        int size = sc.nextInt();

        // Step 2: Outer loop for the rows
        for (int i = 1; i <= size; i++) {
            // Step 3: Print spaces for alignment
            for (int j = i; j < size; j++) {
                System.out.print(" ");
            }
            // Step 4: Print characters in each row
            char ch = 'A';  // Start from character 'A'
            for (int k = 1; k <= i; k++) {
                System.out.print(ch + " ");
                ch++;  // Increment character for the next position
            }
            // Move to the next line after printing each row
            System.out.println();
        }

        // Closing the scanner object
        sc.close();
    }
}

Explanation

Step 1: Input Size

  • The program starts by taking input from the user to determine the size (number of rows) of the pyramid.

Step 2: Outer Loop for Rows

  • The outer loop controls the number of rows to be printed. It iterates from 1 to the size entered by the user.

Step 3: Print Spaces for Alignment

  • The first inner loop prints spaces to ensure the pyramid is centered. The number of spaces decreases as you move down the rows.

Step 4: Print Characters

  • The second inner loop prints characters. It starts with the character A on the first row, and each subsequent row increases the number of characters up to the row number.

Output Example

For size = 5, the output is:

    A
   A B
  A B C
 A B C D
A B C D E

For size = 6, the output is:

     A
    A B
   A B C
  A B C D
 A B C D E
A B C D E F

Conclusion

This Java program prints a character pyramid pattern. The pyramid is aligned using spaces, and characters from A onwards are printed in increasing numbers for each row. This exercise helps in practicing nested loops and character manipulation in Java.

Comments