How to print Floyd's triangle in Java - Example Tutorial

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 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 use this algorithm to find the cycles in linked list. Coming back to Floyd's triangle, it is a right angle triangle which consists natural numbers starting from 1 in the first row. It then goes on with two numbers in second row, 3 numbers in 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 program but help you to build code sense and how to use basic programming constructs e.g. loop, operators and functions.

Floyd's triangle questions is 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 e.g. print Floyd's triangle up-to 5 rows, 10 rows or up to a given number of rows entered by user. We'll implement the last part i.e. our Floyd's triangle will have as many rows as user wants.




Java Program to Print Floyd's triangle

Here is our sample Java program to print Floyd's triangle upto a given number of rows, which is entered by user. We have used the Scanner class to read user input from command prompt, if you are not familiar with how to read user input in Java see here. Scanner class has several benefits e.g. you don't need to read String and parse String into integer, you can directly read integer using nextInt() method.

Once you have number of rows with you, it's very easy to print the Floyd's triangle. You can see we are printing a two dimensional structure, a table, so we need two loop. First loop will print number of rows and the second loop, the inner one will print numbers in each row. If you look closely, numbers in row is in increasing order and doesn't reset between rows. So you just need to keep an integer number outside of loop and keep increasing it on inner loop.

Like the pyramid pattern problem, we need to use both print() and println() method from System.out to print numbers in same row and then switching to next row.

Java Program to print Floyd's triangle


Btw, Rober Floyd's has contributed a lot in the field of computer science and some of his more important work e.g. 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 e.g. Introduction to Algorithms by Thomas Cormen to read more about Floyd's shortest path algorithm and cycle detection algorithm.

How to print Floyd's triangle in Java



Printing Floyd's triangle in Java
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 most easy pattern 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 on learning Floyd's other advanced algorithms e.g. finding shortest path, detecting loops on sequence etc, then please see Introduction to Algorithms by Thomas Cormen. One of the best book to learn about computer algorithms.


No comments:

Post a Comment