Sunday, June 20, 2021
Home coding Write a program to print floyds triangle

Write a program to print floyds triangle

Given a number we have to take input of n and print the floyds triangle of n size.

So what is Floyds Triangle ?

Floyds Triangle:floyds triangle is a right angled triangle formed by natural number which starts from 1 and increases consecutively by each row.

Example:

input:4
output:
1
2 3
4 5 6 
7 8 9 10

Logic:

step1-first print n lines of rows.
step2-take a variable and define it as 1.
step3-increment value by 1 while iterating to each row

Code-

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
  int rows;
  int num = 1;
  cout<<"enter rows"<<endl;
  cin>>rows;
   for (int i = 1; i <= rows; i++) 
   {
      for (int j = 1; j <= i; ++j) 
      {
        cout<<num<<" ";
         num++;
      }
   cout<<endl;
   }

} 

Output:

1
2 3
4 5 6 
7 8 9 10

Time complexity:0(N*N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments