Number Triangle
This program prints a number triangle.
Problem Statement
Write a Java program that prints a number triangle with 5 rows.
Source Code
| 1 | public class NumberTriangle { |
| 2 | public static void main(String[] args) { |
| 3 | for(int i=1; i<=5; 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 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Explanation
The outer loop controls 5 rows and the inner loop prints numbers starting from 1 up to the current row index, so each row increases the maximum printed number by 1, forming a triangle of sequentially growing numbers.