Java Online Compiler
Example: Full Number Pyramid (Centered) in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Full Number Pyramid (Centered) import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Initialize Scanner for user input Scanner scanner = new Scanner(System.in); // Step 2: Prompt user for the number of rows System.out.print("Enter the number of rows for the full pyramid: "); int rows = scanner.nextInt(); // Step 3: Outer loop for the number of rows for (int i = 1; i <= rows; i++) { // Step 4: Inner loop for printing leading spaces for (int space = 1; space <= rows - i; space++) { System.out.print(" "); // Print two spaces for alignment } // Step 5: Inner loop for printing numbers (ascending) for (int j = 1; j <= i; j++) { System.out.print(j + " "); } // Step 6: Inner loop for printing numbers (descending, excluding the peak) for (int j = i - 1; j >= 1; j--) { System.out.print(j + " "); } System.out.println(); // Move to the next line after each row } // Step 7: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS