Floyd's Triangle
This program prints Floyd's Triangle using nested for loop.
Problem Statement
Write a Java program that prints Floyd's Triangle with 5 rows using nested for loop.
Source Code
| 1 | public class FloydTriangle { |
| 2 | public static void main(String[] args) { |
| 3 | int num = 1; |
| 4 | for(int i=1; i<=5; i++) { |
| 5 | for(int j=1; j<=i; j++) { |
| 6 | System.out.print(num + " "); |
| 7 | num++; |
| 8 | } |
| 9 | System.out.println(); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Explanation
The outer loop controls rows, while the inner loop prints increasing numbers in each row. A counter variable num increments sequentially. This demonstrates how nested loops can generate Floyd's Triangle, a structured number sequence. Shows how nested for loops can generate Floyd's Triangle.