Java Online Compiler
Example: Inverted Half Pyramid Pattern (Numbers) in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Inverted Half Pyramid Pattern (Numbers) 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 pyramid: "); int numRows = scanner.nextInt(); // Step 3: Outer loop for rows // It iterates from 'numRows' down to 1, controlling each row of the pyramid. for (int i = numRows; i >= 1; i--) { // Step 4: Inner loop for printing numbers in the current row // It iterates from the current row number 'i' down to 1. // Each iteration prints a number followed by a space. for (int j = i; j >= 1; j--) { System.out.print(j + " "); } // Step 5: Move to the next line after printing all numbers for the current row System.out.println(); } // Step 6: Close the scanner to prevent resource leak scanner.close(); } }
Output
Clear
ADVERTISEMENTS