Program to Print Floyd's Triangle

Last Updated : 11 Mar, 2026

Given an integer n, print Floyd's Triangle with n rows. Floyd's Triangle is a right-angled triangular pattern formed using consecutive natural numbers starting from 1.

Example:

Input: 6

Output:

420851452
Try it on GfG Practice
redirect icon

Using Nested Loops - O(n²) Time and O(1) Space

The pattern can be printed using two nested loops. The outer loop controls the number of rows, while the inner loop prints the numbers in each row. A variable val is used to keep track of the current number, which is incremented after each print so that the numbers appear in increasing order across the triangle.


Step-wise Approach: 

  • Initialize a variable val = 1 to store the current number to print.
  • Run an outer loop from 1 to n to iterate through the rows.
  • For each row i, run an inner loop from 1 to i.
  • Print the current value of val.
  • Increment val after printing each number.
  • After completing each row, move to the next line.
C++
//Driver Code Starts
using namespace std;

//Driver Code Ends

void printfloydtriangle(int n)
{
    int i, j, val = 1;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= i; j++)
            cout << val++ << " ";
        cout << endl;
    }
}

//Driver Code Starts

int main()
{
    printfloydtriangle(6);
    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
class GFG {

//Driver Code Ends

    static void printfloydtriangle(int n)
    {
        int i, j, val = 1;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= i; j++)
                System.out.print(val++ + " ");
            System.out.println();
        }
    }

//Driver Code Starts

    public static void main(String[] args)
    {
        printfloydtriangle(6);
    }
}
//Driver Code Ends
Python
def printfloydtriangle(n):

    val = 1
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(val, end=" ")
            val += 1
        print()



#Driver Code Starts
def main():
    printfloydtriangle(6)


if __name__ == "__main__":
    main()
#Driver Code Ends
C#
//Driver Code Starts
using System;

class GFG
{
//Driver Code Ends

    static void printfloydtriangle(int n)
    {
        int i, j, val = 1;

        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= i; j++)
                Console.Write(val++ + " ");

            Console.WriteLine();
        }
    }


//Driver Code Starts
    static void Main()
    {
        printfloydtriangle(6);
    }
}
//Driver Code Ends
JavaScript
function printfloydtriangle(n)
{
    let i, j, val = 1;

    for (i = 1; i <= n; i++)
    {
        let line = "";

        for (j = 1; j <= i; j++)
        {
            line += val++ + " ";
        }

        console.log(line);
    }
}


// driver code
//Driver Code Starts
printfloydtriangle(6);

//Driver Code Ends

Output: 
 

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
 
Comment