Program to print Floyd’s triangle in C, C++, Java and Python | faceprep

Program to print Floyd’s triangle is discussed here. Floyd’s triangle is a triangle with first natural numbers.

Floyd's triangle

Program to print Floyd's triangle

#include <stdio.h>
int main()
{
int i, j, rows;
printf(“\nEnter the number of rows : “);
scanf(“%d”,&rows);
printf(“\n\nFloyd’s Triangle \n”);
int k = 1;
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf(“%d “,k);
++k;
}
printf(“\n”);
}
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int i, j, rows;
cout << “\nEnter the number of rows : “;
cin >> rows;
cout << “\n\nFloyd’s Triangle \n”;
int k = 1;
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
cout << k << ” “;
++k;
}
cout << endl;
}
return 0;
}

import java.util.*;

class Main
{
public static void main(String args[])
{
int i, j, rows;
System.out.print(“\nEnter the number of rows : “);
Scanner sc = new Scanner(System.in);
rows = sc.nextInt();
System.out.print(“\n\nFloyd’s Triangle \n”);
int k = 1;
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
System.out.print(k + ” “);
k = k + 1;
}
System.out.print(“\n”);
}
}
}

print(“Floyd’s Triangle”)
rows = int(input(“Enter the number of rows : “))
k = 1
for i in range(1,rows+1):
for j in range(1,i+1):
print(k, end = ” “)
k = k + 1
print()

Output:

Floyd's triangle pattern

Time complexity: O(n^2)