Inverted Number Triangle

This program prints an inverted number triangle.

Problem Statement

Write a Java program that prints an inverted number triangle with 5 rows.

Source Code

1 public class InvertedNumberTriangle {
2 public static void main(String[] args) {
3 for(int i=5; i>=1; i--) {
4 for(int j=1; j<=i; j++) {
5 System.out.print(j + " ");
6 }
7 System.out.println();
8 }
9 }
10 }

Program Output

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Explanation

The outer loop starts at 5 and decreases each iteration while the inner loop prints numbers from 1 up to the current row count, so each row prints fewer numbers than the previous, forming an inverted numeric triangle.

Quick Links to Explore