Java Online Compiler
Example: Inverted Right-Angled Number Pyramid in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Inverted Right-Angled Number Pyramid 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 inverted pyramid: "); int rows = scanner.nextInt(); // Step 3: Outer loop for the number of rows (descending) for (int i = rows; i >= 1; i--) { // Step 4: Inner loop to print numbers in each row for (int j = 1; j <= i; j++) { System.out.print(j + " "); // Print number and a space } System.out.println(); // Move to the next line after each row } // Step 5: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS