Java Program to Print Floyd’s Triangle

Floyd’s triangle in Java is a right-angled triangular pattern of numbers in Java, where the numbers from 1 to n are arranged in rows, starting from 1, then 2, and so on. In Java, Floyd’s triangle can be created using nested loops to print the numbers in the correct pattern. The outer loop controls the rows, and the inner loop controls the columns.

Example:
A Floyd’s triangle is a triangle in which each number is the sum of the two numbers above it. For example, the first row of the Floyd’s triangle is 1, the second row is 2 + 1 = 3, and so on. The following is a diagram of the Floyd’s triangle:

1  
2 3  
4 5 6  
7 8 9 10
Problem Description

Write a Java Program to print Floyd’s Triangle.

Problem Solution

1. Take the number of rows as input.
2. Store the number of rows in a variable.
3. Create a for loop to loop through the number of rows.
4. Print the elements for every row.

Let’s discuss different ways to display floyd’s triangle in Java.

advertisement
advertisement
Method 1: Floyds Triangle in Java using For Loop

In Floyd triangle there are n integers in the nth row and a total of (n(n+1))/2 integers in n rows. We take input as number of rows user want to print.

Program/Source Code

Here is the source code of the Java Program to Display Floyd’s Triangle using for loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Print Floyd’s Triangle using for loop
 */
 
import java.util.Scanner;
 
class Floyd
{
   public static void main(String args[])
   {
      int n, num = 1, c, d;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows of floyd's triangle you want");
      n = in.nextInt();
      System.out.println("Floyd's triangle :-");
      for ( c = 1 ; c <= n ; c++ )
      {
         for ( d = 1 ; d <= c ; d++ )
           {
              System.out.print(num+" ");
              num++;
           }
          System.out.println();
      }
   }
}
Program Explanation

1. The program prompts the user to input the number of rows of Floyd’s triangle.
2. The user input is read and stored in the integer variable “n”.
3. A loop is executed from row 1 up to row “n”.
4. Another nested loop is executed for each row from column 1 to the current row “c”.
5. The loop prints the current value of “num” and increments “num” by 1.
6. A new line is printed after the inner loop finishes for each row.
7. The program ends after printing all rows of Floyd’s triangle.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Time complexity: O(n)
The time complexity of displaying the Floyd triangle is O(n).

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, the entered value is “5” to print the floyds triangle.

$ javac Floyd.java
$ java Floyd
 
Enter the number of rows of floyd's triangle you want
5
Floyd's triangle :-
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Testcase 2: In this case, the entered value is “12” to print the floyds triangle.

advertisement
Enter the number of rows: 12
   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 
  56   57   58   59   60   61   62   63   64   65   66 
  67   68   69   70   71   72   73   74   75   76   77   78

Method 2: Floyds Triangle Program in Java using While Loop

In this program, we will use two while loops instead of for loops to print the Floyd’s triangle.

Program/Source Code

Here is the source code of the Java Program to print Floyd’s Triangle using while loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Print Floyd’s Triangle using while loop
 */
 
import java.util.Scanner;
 
class Floyd
{
    public static void main(String args[])
    {
        int n, num = 1, c = 1, d = 1;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        n = in.nextInt();
        System.out.println("Floyd's triangle :-");
 
        while(c <= n)
        {
            while(d <= c)
            {
                System.out.print(num + " ");
                num++;
                d++;
            }
            System.out.println();
            c++;
            d = 1;
        }
    }
}
Program Explanation

In this program, we have used two while loops instead of for loops to print the Floyd’s triangle. The outer while loop runs from 1 to n, which determines the number of rows of the triangle. The inner while loop runs from 1 to the current row number (c), which prints the numbers in each row.

advertisement

The variable num keeps track of the current number to be printed, and the variables c and d are used as counters for the row and column respectively. After each row is printed, the row counter c is incremented, and the column counter d is reset to 1 for the next row.

Time complexity: O(n)
The time complexity of displaying the Floyd triangle is O(n).

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Program Output

In this case, the entered value is “7” to print the floyds triangle.

Enter the number of rows:
7
Floyd's triangle :-
   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

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.