Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Floyd’s triangle

Floyd’s triangle

Floyd’s triangle, named after Robert Floyd, is a right angled triangle which is made up using natural numbers. It start from 1 and consecutively selects the next greater number in the sequence.

In the Floyd’s triangle, there are n integers in the nth row and total of (n(n+1))/2 integer in the rows.  

Floyd's triangle

1. Write a program to print the following pattern

Enter number of row 5                                                                                                      
1                                                                                                                          
2 3                                                                                                                        
4 5 6                                                                                                                      
7 8 9 10                                                                                                                   
11 12 13 14 15

Working

Step1- Take a number of rows to be printed, n.

Step2- Make outer iteration i for n times to print rows.

Step3- Make inner iteration for j to i.

Step4- print k.

Step5- Increment k.

Step6- Print New Line character after each inner iteration.

Step7- Stop.

#include <stdio.h>  
int main()  
{
    int i,j,n,k=1;
    printf("enter number of rows : ");
    scanf("%d",&n);  
    for(i=1;i<=n;i++)
    {  
        for(j=1;j<=i;j++)
        {
           printf("%d ",k);
            k++;
        }
        printf("\n"); 
    }
    return 0;
}  
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//initialize the variable
int i,j,n,k=1;
//taking user input for number of row.
System.out.print("Enter number of row ");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
//declare outer loop for every new row
for(i=1;i<=n;i++)
{
//declare inner loop for print number in row.
for(j=1;j<=i;j++)
{
System.out.print(k+" ");
//increment k because we need to print incremented value
k++;
}
System.out.println();
}
}
}

One comment on “Floyd’s triangle”