
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Floyd's Triangle in Java
In this article, we are going to see how to display Floyd's Triangle using Java. Floyd's triangle is a popular right-angled triangular array consisting of natural numbers. It is formed by starting with the number 1 at the top of the triangle, and then incrementing each subsequent number by 1 as you move down the rows of the triangle.
The first row contains only 1 number which is 1 itself and each subsequent row contains 1 more number than the previous row. The triangle has n rows where n can be any positive whole number.
The total number of values in the triangle will be the sum of the 1st n natural numbers which is calculated using the formula S = n/2 * (2a + (n-1) d) where S is the sum of the series, n is the number of terms in the series, a is the first term in the series, d is the common difference between the terms.
However, in Floyd's triangle, the 1st term is always 1 and the common difference is 1 so we can simplify this formula to:
S= n/2 * (2 + n - 1) = n/2 * (n+1)
Hence, the number of values in Floyd's triangle with n rows is n/2 * (n+1).
If there are 5 rows i.e. n=5 then the total number of values in the triangle is:
5/2 * (5+1) = 15
Problem Statement
Write a program in Java to display Floyd's triangle.
Input
rows = 5 (Number of rows n)
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Algorithm for Floyd's triangle
Below is the algorithm to display Floyd's triangle ?
- Input the number of rows n.
- Initialize a variable number to 1.
- For each row i from 1 to n, do the following:
- For each element j in the row (from 1 to i), do the following:
- i. Print the current value of number.
- ii. Increment number by 1.
- Print a newline character to move to the next row.
Different Approaches
Following are the different approaches to display Floyd's triangle ?
Using for loops
For loops are a type of control flow statement that executes a set of instructions repeatedly. It contains 3 parts which are an initialization statement, a Boolean condition, and an update statement. After the loop body is executed the update statement is executed and the condition is checked again until the Boolean condition becomes false.
Example
The Java implementation to display Floyd's triangle using nested for loops is given below ?
public class FloydTriangle { public static void main(String[] args) { // declare the number of rows int rows = 5; int num = 1; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(num + " "); num++; } System.out.println(); } } }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Using while loops
While loops are another form of control flow statements that repeatedly execute based upon a pre-defined Boolean condition and terminate itself once the condition is false.
Example
public class FloydTriangle { public static void main(String[] args) { int rows = 5; int number = 1; int i = 1; while (i <= rows) { int j = 1; while (j <= i) { System.out.print(number + " "); number++; j++; } System.out.println(); i++; } } }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Using do-while loop
A do-while is pretty much like a while loop except that it gets executed at least once. This is because the condition is tested at the end of every iteration. If the condition is true, the loop will continue and terminate itself once it is false. Here, the number of rows was pre-defined to 10.
Example
public class FloydTriangle { public static void main(String[] args) { int rows = 10; int number = 1; int i = 1; do { int j = 1; do { System.out.print(number + " "); number++; j++; } while (j <= i); System.out.println(); i++; } while (i <= rows); } }
Output
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
Conclusion
In conclusion, Floyd's triangle is a right-angled triangle made up of natural numbers, with each row containing an increasing number of elements. You can generate Floyd's triangle in Java using different types of loops like for, while, and do-while loops. Each loop version follows a simple logic: starting from 1, the number is printed and incremented as you move through each row. This process continues until the triangle is formed with the desired number of rows, making the implementation both straightforward and efficient.