Floyd’s triangle is a right-angled triangle of natural numbers, which is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner, there are n integers in the nth row and a sum of (n(n+1))/2 integers in n rows. Write a java program to print Floyd’s triangle is one of the popular java interview questions.
Floyd’s Triangle in java
package com.javainterviewpoint; import java.util.Scanner; /** * @author JavaInterviewPoint * */ public class FloydTriangle { /** * @param args */ public static void main(String[] args) { //Create a new Scanner object Scanner scanner = new Scanner(System.in); //Get the number of rows from the user System.out.println("Enter the number of row to print"); int rows = scanner.nextInt(); int floydNumber=1; System.out.println("** Printing **"); for(int i=1;i<=rows;i++) { for(int j=1;j<=i;j++) { System.out.print(floydNumber+" "); floydNumber = floydNumber + 1; } System.out.println(); } } }
- Get the number of rows which needs to be printed from the user
- We have two loops, the first loop is used for the number of the rows and second loop is for printing the floydNumber and incrementing it by 1
Output
Enter the number of row to print 9 ** Printing ** 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
Floyd Triangle with stars in Java
package com.javainterviewpoint; import java.util.Scanner; /** * @author JavaInterviewPoint * */ public class FloydTriangle { /** * @param args */ public static void main(String[] args) { //Create a new Scanner object Scanner scanner = new Scanner(System.in); //Get the number of rows from the user System.out.println("Enter the number of row to print"); int rows = scanner.nextInt(); int floydNumber=1; System.out.println("-- Printing --"); for(int i=1;i<=rows;i++) { for(int j=1;j<=i;j++) { System.out.print("* "); floydNumber++; } System.out.println(); } } }
Output:
Enter the number of row to print 5 -- Printing -- * * * * * * * * * * * * * * *
Reverse Floyd’s Triangle in Java / Inverted Triangle
package com.javainterviewpoint; import java.util.Scanner; /** * @author JavaInterviewPoint * */ public class ReverseFloydsTriangle { /** * @param args */ public static void main(String[] args) { //Create a new Scanner object Scanner scanner = new Scanner(System.in); //Get the number of rows from the user System.out.println("Enter the number of row to print"); int rows = scanner.nextInt(); int floydNumber = rows * (rows+1) / 2; System.out.println("** Printing **"); for(int i=rows;i>=1;i--) { for(int j=1;j<=i;j++) { System.out.print(floydNumber+" "); floydNumber = floydNumber - 1; } System.out.println(); } } }
Output:
Enter the number of row to print 7 ** Printing ** 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Floyd Triangle in Java using while loop
package com.javainterviewpoint; import java.util.Scanner; /** * @author JavaInterviewPoint * */ public class FloydTriangle { /** * @param args */ public static void main(String[] args) { //Create a new Scanner object Scanner scanner = new Scanner(System.in); //Get the number of rows from the user System.out.println("Enter the number of row to print"); int rows = scanner.nextInt(); int floydNumber=1; System.out.println("-- Printing --"); int i =1; int j= 1; while (i <=rows) { j=1; while (j<=i) { System.out.print(floydNumber+" "); floydNumber++; j++; } System.out.println(); i++; } } }
Output:
Enter the number of row to print 7 -- Printing -- 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
Leave a Reply