C++ program to print Floyd’s triangle

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

Example: Floyd’s triangle in C++

//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

cpp program to print Floyd triangle

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

Recommended Articles

C++ For Loop

C++ Input / Output