Monday, March 30, 2015

How to print Floyd's Triangle in Java with Example

How to print Floyd's Triangle in Java with Example

There are lots of programming exercise in Java, which involves printing a particular pattern in console, one of them is printing Floyd triangle in console. In Floyd triangle there are n integers in the nth row and a total of (n(n+1))/2 integers in n rows. This is one of the most simple pattern to print but helpful in learning how to create other more complex patterns. Key to develop pattern is using nested loops and methods like System.out.print() and println() appropriately. Actually pattern based programming task are originally designed to master loops in programming. A good programmer should be able to look a pattern and break into nested loops. A more formal definition of Floyd's triangle : "It's a right angled triangle of array of natural numbers, which is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, stating with 1 in the top left corner".

It looks like following pattern :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Your task is to print this table using nested loops in Java. Main point is to build the logic by yourself, this will help you a lot in the long run.


Java Program to draw Floyd's Triangle

Here is my sample code example to draw format representing Floyd's triangle. This  program first ask user to enter number of rows till you want to show Floyd's triangle. You can use Scanner to get the input from user and then you can use that number in your logic. The code for printing Floyd's triangle is the method printFloydTriangle(int rows), it takes number of rows, which is the user input. This method uses nested loops, two loop in this case to draw format of Floyd's triangle. First loop is used to print number of rows and second loop is used to print numbers in the row.


import java.util.Scanner;

/**
* Java program to print Floyd's triangle up-to a given row
*
* @author Javin Paul
*/
public class FloydTriangle {

public static void main(String args[]) {
Scanner cmd = new Scanner(System.in);

System.out.println("Enter the number of rows of Floyd's triangle, you want to display");
int rows = cmd.nextInt();
printFloydTriangle(rows);

}

/**
* Prints Floyd's triangle of a given row
*
* @param rows
*/
public static void printFloydTriangle(int rows) {
int number = 1;
System.out.printf("Floyd's triangle of %d rows is : %n", rows);

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
number++;
}

System.out.println();
}
}

}

Output
Enter the number of rows of Floyd's triangle, you want to display
5
Floyd's triangle of 5 rows is :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Enter the number of rows of Floyd's triangle, you want to display
10
Floyd's triangle of 10 rows is :
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

That's all about how do you print Floyd's triangle in Java. It's a very good programming exercise to build your foundation on loops especially nested loops. After that you can also try couple of other pattern based programming task's e.g. printing Pascal's triangle and some other patterns. If you like to do solve some programming problems other than patterns you can also try following exercises :


No comments:

Post a Comment