Skip to content

Commit 049c5c7

Browse files
committedSep 21, 2019
Floyd's Triangle Pattern
1 parent e9ebf5b commit 049c5c7

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.java.patterns;
2+
3+
import java.util.Scanner;
4+
/*
5+
* Floyd's Triangle
6+
*
7+
* Floyd's triangle is a right-angled triangular array of natural numbers,
8+
* used in computer science education. It is named after Robert Floyd.
9+
* It is defined by filling the rows of the triangle with consecutive numbers,
10+
* starting with a 1 in the top left corner:
11+
1
12+
2 3
13+
4 5 6
14+
7 8 9 10
15+
11 12 13 14 15
16+
16 17 18 19 20 21
17+
22 23 24 25 26 27 28
18+
29 30 31 32 33 34 35 36
19+
37 38 39 40 41 42 43 44 45
20+
46 47 48 49 50 51 52 53 54 55
21+
*/
22+
public class FloydsTriangle {
23+
public static void main(String[] args) {
24+
Scanner scanner = new Scanner(System.in);
25+
System.out.println("Enter the number of rows of Floyd's Triangle to print :: ");
26+
int rows = Integer.parseInt(scanner.nextLine().trim());
27+
28+
//logic to print the Floyd's triangle
29+
int value = 1;
30+
for(int i=0;i<rows;i++){
31+
for(int j=0;j<(i+1);j++,value++)
32+
System.out.print(value+" ");
33+
System.out.println();
34+
}
35+
36+
scanner.close();
37+
}
38+
}
39+
/*
40+
Enter the number of rows of Floyd's Triangle to print :: 5
41+
1
42+
2 3
43+
4 5 6
44+
7 8 9 10
45+
11 12 13 14 15
46+
47+
Enter the number of rows of Floyd's Triangle to print :: 10
48+
1
49+
2 3
50+
4 5 6
51+
7 8 9 10
52+
11 12 13 14 15
53+
16 17 18 19 20 21
54+
22 23 24 25 26 27 28
55+
29 30 31 32 33 34 35 36
56+
37 38 39 40 41 42 43 44 45
57+
46 47 48 49 50 51 52 53 54 55
58+
59+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.