Floyd Triangle in Java
Print Floyd’s triangle pattern using consecutive numbers.
BeginnerModule 3: Loop ProgramsExample 17 of 25
floyd-triangle.java
1public class Main {2 public static void main(String[] args) {3 int n = 5;4 int num = 1;5 for (int i = 1; i <= n; i++) {6 for (int j = 1; j <= i; j++) {7 System.out.print(num++ + " ");8 }9 System.out.println();10 }11 }12}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
What's going on
We maintain a running number that increments each time we print in the nested loop.