Write a program to print Floyd’s triangle in C++

In this tutorial, we are going to learn how to write a program to print Floyd’s triangle in C++. This is a printing pattern challenge problem which we are going to solve using C++. Let us first see what is Floyd’s triangle?

Floyd’s triangle

Floyd’s triangle is a triangle having first natural numbers which are printed according to the following pattern. Pattern length increases one by one after each iteration.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28

Let us try to implement our above idea in our code and try to print the required output. These types of problems are basically asked in company exams like TCS which just to test your basic coding skills.

Program to print Floyd’s triangle in C++

C++ source code:

// Progrma to print Floyd's triangle
#include<bits/stdc++.h>
using namespace std;
int main()
{
int rows;
cout<<"\nEnter the number of rows you want to be in Floyd's triangle: ";
cin>>rows;
cout<<"\nFloyd's Triangle \n";
int k=1;
for(int i=1;i<=rows; ++i)
{
    for(int j=1; j<=i; ++j)
  {
      cout<<k<<" ";
    ++k;
  }
  cout<<"\n";
}
return 0;
}

Input/Output:

Enter the number of rows you want to be in Floyd's triangle: 7

Floyd's Triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28

You can also learn:

Boundary traversal of the Binary tree in C++




Creating a Variable and Appending it in Python

Do not forget to comment if you find anything wrong in the post or you want to share some information regarding the same.


Leave a Reply

Your email address will not be published. Required fields are marked *