Write a Java Program to print Floyd’s Triangle with example. The Floyd’s Triangle is a right angled triangle with an array of natural numbers.
Java Program to print Floyd’s Triangle
This Java program allows the user to enter the maximum number of rows he/she want to print as Floyd’s triangle. Then, this program prints Floyd’s triangle of natural numbers until it reaches the user-specified rows.
// Java Program to print Floyd’s Triangle package ShapePrograms; import java.util.Scanner; public class FloydTriangle { private static Scanner sc; public static void main(String[] args) { int rows, i, j, number = 1; sc = new Scanner(System.in); System.out.println(" Please Enter the Number of Rows you wish to see in FloydTriangle: "); rows = sc.nextInt(); System.out.println("---- Printing FLOYD'S Triangle ------"); for ( i = 1 ; i <= rows; i++ ){ for ( j = 1 ; j <= i; j++ ) { System.out.format("%d ", number); number++; } System.out.println(""); } } }

The first two statements will allow the User to enter the range or maximum Number of rows he/she want to print.
Now let us see the Nested for loop in Java in the iteration wise.
Outer For Loop – First Iteration
From the above screenshot, please observe that the value of i is 1 and Rows is 5. So, the condition (i <= 5) is True, and it will enter into second for loop.
Inner For Loop – First Iteration
The j value is 1, and the condition (j <= 1) is True. So, it will start executing the statements inside the loop. The System.out.format statement will print Number as Output, and we all know that Number = 1. So, 1 printed.
The below number++ statement will increment the Value of Number by 1 using the Increment Operator
Inner For Loop – Second Iteration: The j value will be 2, and the condition (2 <= 1) is False so that it will exit from the second loop.
The System.out.println statement is to terminate the current line.
Outer For Loop – Second Iteration: The value of i will be 2, and the condition (2 <= 5) is True. So, it will enter into second for loop.
Inner For Loop – First Iteration
The value of j is 1 ,and the condition (1 <= 2) is True. So, it will start executing the statements inside the loop. It will print Number as Output, and we all know that Number = 2. So, it prints 2.
increment the Number by 1
Next, j value will also be incremented by 1.
Inner For Loop – Second Iteration: The value of j is 2, and the condition (2 <= 2) is True. So, it will start executing the statements inside the loop. It means 3 printed.
Inner For Loop – Third Iteration: The j value will be 3, and the condition (3 <= 2) is False. So, it exits from the second loop.
It happens until it reaches 5, and after that, both the Inner and Outer loop terminated.
Java Program to Print Floyd’s Triangle using Functions
This program allows the user to enter the maximum number of rows to print as Floyd’s triangle. Next, this program prints Floyd’s triangle of natural numbers using Functions.
package ShapePrograms; import java.util.Scanner; public class FloydTriangleusingMethods { private static Scanner sc; public static void main(String[] args) { int rows; sc = new Scanner(System.in); System.out.println(" Please Enter the Number of Rows you wish to see in FloydTriangle: "); rows = sc.nextInt(); System.out.println("---- Printing ------"); FloydTriangle(rows); } public static void FloydTriangle (int rows) { int i, j, number = 1; for ( i = 1 ; i <= rows; i++ ) { for ( j = 1 ; j <= i; j++ ) { System.out.format("%d ", number); number++; } System.out.println(""); } } }
Java Floyd’s Triangle output
Please Enter the Number of Rows you wish to see in FloydTriangle:
10
---- Printing ------
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
When the compiler reaches to FloydTriangle(rows); line then the compiler will immediately jump to below function:
public static void FloydTriangle (int rows) {
We already explained the LOGIC in the above example.
Java Program to Print Floyd’s Triangle without Natural numbers
This Java program allows the user to enter the number of rows he/she want to display as Floyd’s triangle. In this example, we are going to print Floyd’s using the * symbol. This Java program can also say as printing Right angled triangle with * symbols.
// Floyd’s Triangle program in Java package ShapePrograms; import java.util.Scanner; public class FloydTrianglewithoutNumbers { private static Scanner sc; public static void main(String[] args) { int rows, i, j; sc = new Scanner(System.in); System.out.print(" Please Enter the Number of Rows you wish to see : "); rows = sc.nextInt(); System.out.println("---- Printing ------"); for ( i = 1 ; i <= rows; i++ ) { for ( j = 1 ; j <= i; j++ ) { System.out.format("* "); } System.out.println(""); } } }
Please Enter the Number of Rows you wish to see : 8
---- Printing ------
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *