Floyd Triangle in Java
Floyd Triangle in Java
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
import java.util.Scanner;
public class FloydsTriangle
{
public static void main(String[] args)
{
System.out.println("How many rows you want in Floyd's Triangle?");
Scanner sc = new Scanner(System.in);
int noOfRows = sc.nextInt();
int value = 1;
System.out.println("Floyd's Triangle : ");
for (int i = 1; i <= noOfRows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(value+"\t");
value++;
}
System.out.println();
}
}
27
28
29
Output :
?
1
2
3
4
5
6
7
8
How it works?
Let noOfRows = 5 and value = 1.
1st iteration :
for i = 1
for j = 1
Print value > Print 1
value++ > value = 2
for j = 2
As j > i
break inner for loop
Go To Next Line
After 1st iteration, output will be,
?
2nd iteration :
for i = 2
for j = 1
Print value > Print 2
value++ > value = 3
for j = 2
Print value > Print 3
value++ > value = 4
for j = 3
As j > i
break inner for loop
Go To Next Line
After 2nd iteration, output will look like below,
?
1
2
1
2
3rd iteration :
for i = 3
for j = 1
Print value > Print 4
value++ > value = 5
for j = 2
Print value > Print 5
value++ > value = 6
for j = 3
Print value > Print 6
1
2
3
1
2
4
3
5
4th iteration :
for i = 4
for j = 1
Print value > Print 7
value++ > value = 8
for j = 2
Print value > Print 8
value++ > value = 9
for j = 3
Print value > Print 9
value++ > value = 10
for j = 4
Print value > Print 10
value++ > value = 11
for j = 5
As j > i
break inner for loop
Go To Next Line
After 4th iteration, output will be,
?
1
2
3
4
1
2
4
7
3
5
8
6
9
10
5th iteration :
for i = 5
for j = 1
Print value > Print 11
value++ > value = 12
for j = 2
Print value > Print 12
value++ > value = 13
for j = 3
Print value > Print 13
value++ > value = 14
for j = 4
Print value > Print 14
value++ > value = 15
for j = 5
1
2
3
4
5
1
2
4
7
11
3
5 6
8 9 10
12 13 14 15
6th Iteration :
for i = 6
As i > noOfRows
Break outer for loop
Final Output :
?
1
2
3
4
5
6
7
8