C++ program to print Floyd triangle

In this example, you will learn a c++ program to print the Floyd triangle on the screen.  Floyd triangle is a right angle triangle of natural numbers whose all sides are equal. This program takes maximum rows from the user and prints the Floyd triangle.

Prerequisite

To understand this program perfectly you should have the following C++ programming knowledge.

C++ for loop

C++ operators

Program: Print Floyd Triangle

//C++ program to print Floyd triangle
#include <iostream>
using namespace std;

int main()
{
int totalRows, j,  k, count = 1;
    
cout <<"Enter total number of rows to print: ";
cin >> totalRows;
    
for (j = 1; j <= totalRows; j++)
{
for (k = 1; k <= j; k++)
{        
cout << count; 
count++; 
}
cout << endl;
}    
return 0;
}

Output

Enter total number of rows to print: 5
1
23
456
78910
1112131415

Description and working on this program

  • This program takes a total number of rows from the user and stores it in the “totalRows” variable.
  • Initialize for loop for the total number of rows.
  • Initialize another loop to print natural numbers in each row.
  • Terminate program