Hello guys, In the last article, I have taught you how to print Pascal's triangle and in today's article, I'll teach you how to print Floyd's triangle in the Java program. Floyd's triangle is easier to print than Pascal's triangle because you don't need to take care of formatting the numbers as Floyd's triangle is a right-angle triangle. It is named after American computer scientist Robert Floyd, who has also contributed Floyd–Warshall algorithm, which efficiently finds all shortest paths in a graph, and Floyd's cycle-finding algorithm for detecting cycles in a sequence.
If you remember, we have used this algorithm previously to find the cycles in the linked list. Coming back to Floyd's triangle, it is a right-angle triangle that consists of natural numbers starting from 1 in the first row.
It then goes on with two numbers in the second row, 3 numbers in the 3rd row, and so on. Along with other pattern-based exercises and Pascal's triangle, Floyd's triangle is also a good programming exercise and often used in programming and training courses to teach how to program to beginners.
It's one of the easier programs but help you to build code sense and how to use basic programming constructs like loop, operators, and functions.
Floyd's triangle questions are also asked on computer programming practical exams as, Can you write a Java program to print Floyd's triangle of 5 rows as shown below:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Sometimes with additional constraints like print Floyd's triangle up to 5 rows, 10 rows, or up to a given number of rows entered by the user. We'll implement the last part i.e. our Floyd's triangle will have as many rows as the user wants.
Floyd's triangle questions are also asked on computer programming practical exams as, Can you write a Java program to print Floyd's triangle of 5 rows as shown below:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Sometimes with additional constraints like print Floyd's triangle up to 5 rows, 10 rows, or up to a given number of rows entered by the user. We'll implement the last part i.e. our Floyd's triangle will have as many rows as the user wants.
Java Program to Print Floyd's Triangle for Given Row
Here is our sample Java program to print Floyd's triangle up to a given number of rows, which is entered by the user. We have used the Scanner class to read user input from the command prompt, if you are not familiar with how to read user input in Java sees here.Scanner class has several benefits like you don't need to read String and parse String into an integer, you can directly read integer using nextInt() method.
Once you have a number of rows with you, it's very easy to print Floyd's triangle. You can see we are printing a two-dimensional structure, a table, so we need two loops. The first loop will print a number of rows and the second loop, the inner one will print numbers in each row.
Once you have a number of rows with you, it's very easy to print Floyd's triangle. You can see we are printing a two-dimensional structure, a table, so we need two loops. The first loop will print a number of rows and the second loop, the inner one will print numbers in each row.
If you look closely, the numbers in a row are in increasing order and don't reset between rows. So you just need to keep an integer number outside of the loop and keep increasing it on the inner loop.
Like the pyramid pattern problem, we need to use both print() and println() method from System.out to print numbers in the same row and then switching to the next row.
Like the pyramid pattern problem, we need to use both print() and println() method from System.out to print numbers in the same row and then switching to the next row.
Btw, Rober Floyd's has contributed a lot in the field of computer science, and some of his more important work like Floyd–Warshall algorithm to efficiently finds all shortest paths in a graph and Floyd's cycle-finding algorithm for detecting cycles in a sequence are often taught in data structure and algorithm classes.
You can read a good algorithm book like Introduction to Algorithms by Thomas Cormen to read more about Floyd's shortest path algorithm and cycle detection algorithm.
You can read a good algorithm book like Introduction to Algorithms by Thomas Cormen to read more about Floyd's shortest path algorithm and cycle detection algorithm.
Here is our complete Java program to print Floyd's triangle. This program takes input from the user about how many rows of the triangle need to be printed and then print accordingly.
That's all about how to print Floyd's triangle in Java. You can see it's not difficult, in fact, it's one of the easiest patterns you would get to print from Java program but for beginners, this program really helps them to understand basic coding techniques e.g. using loop, operator. If you are more interested in learning Floyd's other advanced algorithms like finding the shortest path, detecting loops on sequence, etc, then please see the following courses.
Other Common Programming Exercises You may want to check
import java.util.Scanner; /* * Java Program to print Floyd's triangle as shown below: * 1 * 2 3 * 4 5 6 * 7 8 9 10 * 11 12 12 14 15 */ public class FloydTriangleInJava { public static void main(String[] args) { System.out.println("Welcome to Java program to print Floyd's triangle"); System.out.println("Please enter the number of rows of " + "Floyd's triangle you want to print"); Scanner scnr = new Scanner(System.in); int rows = scnr.nextInt(); printFloydTriangle(rows); scnr.close(); } /* * Java method to print Floyd's triangle upto given * number of rows. */ public static void printFloydTriangle(int numberOfRows) { int number = 1; for (int row = 1; row <= numberOfRows; row++) { for (int column = 1; column <= row; column++) { System.out.print(number + " "); number++; } System.out.println(); } } } Output Welcome to Java program to print Floyd's triangle Please enter the number of rows of Floyd's triangle you want to print 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Welcome to Java program to print Floyd's triangle Please enter the number of rows of Floyd's triangle you want to print 7 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
That's all about how to print Floyd's triangle in Java. You can see it's not difficult, in fact, it's one of the easiest patterns you would get to print from Java program but for beginners, this program really helps them to understand basic coding techniques e.g. using loop, operator. If you are more interested in learning Floyd's other advanced algorithms like finding the shortest path, detecting loops on sequence, etc, then please see the following courses.
Other Common Programming Exercises You may want to check
- How to find all pairs in an array whose sum is equal to k (solution)
- My favorite free courses to learn data Structure in-depth (FreeCodeCamp)
- How to find duplicates from an unsorted array in Java? (solution)
- How to find one missing number in a sorted array? (solution)
- Recursive InOrder traversal Algorithm (solution)
- 50+ Data Structure and Algorithms Problems from Interviews (questions)
- How to find a missing value from an array containing 1 to 100? (solution)
- How to count the number of leaf nodes in a given binary tree in Java? (solution)
- How to remove an element from an array in Java? (solution)
- How to check if an array contains a particular value? (solution)
- Iterative PreOrder traversal in a binary tree (solution)
- 10 Free Data Structure and Algorithm Courses for Programmers (courses)
- How to remove duplicates from an array in Java? (solution)
- How to find the largest and smallest number in an array without sorting? (solution)
- 100+ Data Structure Coding Problems from Interviews (questions)
Thanks for reading this article so far. If you find this coding exercise useful then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are looking for some Free Algorithms courses to learn Data Structure and Algorithms, from scratch in a guided and hands-on way then you should also check the Data Structures in Java for Beginners [FREE] course on Udemy. It's completely free and you just need an Udemy account to join this course.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.