Q. Write an algorithm and program to print Floyd's triangle.

Solution : Here you will find an algorithm and program to print Floyd's triangle. Given a numbers of line n and our task is to print Floyd's triangle.

Floyd's triangle :- Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education.It is named after Rober Floyd. Floyd's triangle starts from 1 and consecutively selects the next greater number in sequence.
For Example :-
Input : Enter the Floyd's triangle lines: 4
Output :
1
2 3
4 5 6
7 8 9 10
Explanation : As we can see that input triangle lines: N = 4. The output should look like Floyd's triangle. In this, we are using nested loop to print traingle. Outer loop i will start from 1 to n and inner loop j will start from 1 to i+1.

Floyd's Triangle Algorithm

//Algorithm To Print Floyd's triangle
START
Step 1 ->  Take Floyd's triangle input line n.
Step 2 ->  Set val=1;
Step 3 ->  for loop i to n:
for loop j to i+1:
print val
val+=1 
loop end
Give one line space using.
loop end                               
STOP


Floyd's Triangle Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Print Floyd's Triangle.
    #include <stdio.h>
    int main(){ 
    int i,j=1,n;
    printf("Enter the Floyd's triangle lines :");
    scanf("%d",&n);
    for (i=1;i<=(n*(n+1))/2;i++){ 
    printf("%d ",i); 
    if(i==(j*(j+1))/2){ 
    printf("\n"); 
    j++; 
    } 
    }   
    return 0;
    } 
    
    //C++ Program To Print Floyd's Triangle.
    #include <iostream>
    using namespace std; 
    int main() 
    { 
    int i,j=1,n;
    cout<<"Enter the Floyd's triangle lines :";
    cin>>n;
    for (i=1;i<=(n*(n+1))/2;i++){ 
    cout<<i<<" "; 
    if(i==(j*(j+1))/2){ 
    cout<<"\n"; 
    j++; 
    } 
    }  
    return 0; 
    }
    
    //Java Program To Print Floyd's Triangle.
    import java.util.Scanner;
    public class LFC
    {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter the Floyd's triangle lines :");
    int n = scan.nextInt();
    int i, j, val = 1; 
    for (i = 1; i <= n; i++) 
    { 
    for (j = 1; j <= i; j++) 
    { 
    System.out.print(val + " "); 
    val++; 
    } 
    System.out.println(); 
    } 
    }
    }
    
    //Python Program To Print Floyd's Triangle.
    print('Enter the Floyd's triangle lines :')
    n = int(input())
    val = 1
    for i in range(1, n + 1): 
    for j in range(1, i + 1): 
    print(val, end = " ") 
    val += 1
    print("") 
    
    //C# Program To Print Floyd's Triangle.
    using System;
    class LFC {
    static void Main() {
    int i, j, val = 1;
    Console.Write("Enter the Floyd's triangle lines :");      
    int n= int.Parse(Console.ReadLine());  
    for (i = 1; i <= n; i++) 
    { 
    for (j = 1; j <= i; j++) 
    { 
    Console.Write(val + " "); 
    val++; 
    } 
    Console.WriteLine(); 
    } 
    }
    }
    //PHP Program To Print Floyd's Triangle.
    <?php
    $n=4;
    $val = 1; 
    // loop for number of lines 
    for($i = 1; $i <= $n; $i++) 
    { 
    // loop for number of elements 
    // in each line 
    for($j = 1; $j <= $i; $j++) 
    { 
    print($val." "); 
    $val++; 
    } 
    print("\n"); 
    } 
    ?>
    //C Program To Print Floyd's Triangle.
    #include <stdio.h>
    int main(){ 
    int i,j=1,n;
    printf("Enter the Floyd's triangle lines :");
    scanf("%d",&n);
    for (i=1;i<=(n*(n+1))/2;i++){ 
    printf("%d ",i); 
    if(i==(j*(j+1))/2){ 
    printf("\n"); 
    j++; 
    } 
    }   
    return 0;
    } 
    

    Output

    Enter the Floyd's triangle lines :4
    1
    2 3
    4 5 6
    7 8 9 10
    

    Recommended Programs

       Program to sort an array element in descending order using Bubble Sort.
       Program to find largest element in an array.