Java Online Compiler
Example: Centered Symmetric Alphabet Pyramid in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Centered Symmetric Alphabet Pyramid import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Get the number of rows from the user System.out.print("Enter the number of rows for the pyramid: "); int rows = scanner.nextInt(); // Step 2: Outer loop for each row for (int i = 0; i < rows; i++) { // Step 3: Inner loop to print leading spaces for centering for (int j = 0; j < rows - 1 - i; j++) { System.out.print(" "); } // Step 4: Inner loop to print increasing alphabets (A, B, C...) for (int k = 0; k <= i; k++) { System.out.print((char) ('A' + k)); } // Step 5: Inner loop to print decreasing alphabets (..., C, B) for (int l = i - 1; l >= 0; l--) { System.out.print((char) ('A' + l)); } // Step 6: Move to the next line after each row is complete System.out.println(); } scanner.close(); } }
Output
Clear
ADVERTISEMENTS