Java Online Compiler
Example: Palindromic Pyramid Pattern in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Palindromic Pyramid Pattern import java.util.Scanner; // Included as per template, though not strictly used for input here // Main class containing the entry point of the program public class Main { public static void main(String[] args) { int n = 5; // Number of rows for the pyramid // Outer loop for rows // Step 1: Iterate from 1 to 'n' for each row for (int i = 1; i <= n; i++) { // Inner loop for leading spaces // Step 2: Print spaces before the numbers to form a pyramid shape // The number of spaces decreases with each row for (int j = 1; j <= n - i; j++) { System.out.print(" "); // Print two spaces for alignment } // Inner loop for ascending numbers // Step 3: Print numbers from 1 up to the current row number 'i' for (int j = 1; j <= i; j++) { System.out.print(j + " "); // Print the number followed by a space } // Inner loop for descending numbers (excluding the peak 'i') // Step 4: Print numbers from (i-1) down to 1 for (int j = i - 1; j >= 1; j--) { System.out.print(j + " "); // Print the number followed by a space } // Step 5: Move to the next line after completing each row System.out.println(); } } }
Output
Clear
ADVERTISEMENTS