
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Display Floyd's Triangle
Floyd's triangle is a popular right-angled triangular array consisting of natural numbers. The name comes from its founder Robert W. Floyd who was a prominent computer scientist. 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.
In this article, we are going to see how to display Floyd’s Triangle with the help of a Java program.
But before going forward with the Java implementation, let us understand Floyd’s triangle a little more.
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 a 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 total number of values in a 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
Algorithm
Input: Number of rows n
1. Initialize a variable "number" to 1
2. For i from 1 to n, do the following −
a. For j from 1 to i, do the following −
i. Print the value of "number"
ii. Increment "number" by 1
b. Print a newline character to move to the next row
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.
The java implementation to display Floyd’s triangle using nested for loops is given below.
Example
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
The above program will produce the following 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 which repeatedly execute based up on a pre-defined Boolean condition and terminates 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
The above program will produce the following 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, for the simple reason the condition is tested at the end of every iteration. If the condition is true, the loop will continue and terminate itself once the condition 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
The above program will produce the following 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
The Floyd's triangle is a simple example used to demonstrate and practice basic concepts like loops and patterns. It is not limited to a Java implementation and is often used to teach C++, java, C# and many more programming languages. The triangle consists of n rows where n can be pre-defined and stored in an integer while writing the code. It can further be optimized to ask the user to enter the value of n or number of rows (with the help of scanner class or any other input methods) and this would give an even better practice to the learners. On the whole, this program is a simple and efficient way to generate Floyd's triangle in Java. Care should be taken while defining the condition of the loops as it can enter an infinite loop.
- Related Articles
- Java program to display English alphabets
- Java Program to display printable characters
- Java Program to Display Fibonacci Series
- Java Program to display upper triangular matrix
- Java Program to display previous month from GregorianCalendar
- Java Program to display previous day from GregorianCalender
- Java Program to display previous year from GregorianCalendar
- Java Program to display past date from GregorianCalendar
- Java Program to display computer time in milliseconds
- Java Program to display the contents in JTextArea
- Java Program to display Frame after some seconds
- Java Program to display sub-list of ArrayList
- Java Program to display a webpage in JEditorPane
- Java Program to display Bit manipulation in Integer
- Java Program to display JTabbedPane on the right
