Signup/Sign In

C++ Modified Floyd's Triangle Program

Hello Everyone!

In this tutorial, we will learn how to print the Modified Floyd's Triangle, in the C++ programming language.

What's a Floyd's Triangle?

Floyd's triangle is a right-angled triangular array of natural numbers. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

In Modified Floyd's Triangle, each row starts with the row number, printing all the consecutive numbers and contains no. of columns equal to the row number. The below code as well as the output snippet will help you understand the mentioned definition.

All such patterns using * or alphabets or numbers are achieved by making use of the nested loop structures by knowing how to iterate and till where to iterate.

We believe that all the patterns covered in this section will help you understand this concept and visualize it better while forming your own patterns, as such questions are very frequently asked in various interviews with a slight modification.


#include <iostream>
using namespace std;

int main()
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to print Floyd's Triangle ===== \n\n";

    //i to iterate the outer loop and j for the inner loop
    int i, j, rows;

    //to denote the range of numbers in each row
    int n=0, first,last; 

    cout << "Enter the number of rows in the pyramid: ";
    cin >> rows;

    cout << "\n\nThe required Floyd's Triangle containing " << rows << " rows is:\n\n";

    //outer loop is used to move to a particular row
    for (i = 1; i <= rows; i++)

        first = i;
        last  = first + i -1;
        //to display that the outer loop maintains the row number
        //cout << "Row # " << i << " contains the numbers from " << first << " to " << last << " :    ";

        //inner loop is used to decide the number of columns in a particular row
          for (j = 1; j <= i; ++j) // remember: in such cases, ++j works same as j++ (but not always- we will cover this in upcoming posts)
            cout << n + j << " ";

        cout << endl; //endl works same as '\n'

    cout << "\n\n";

    return 0;

You may get a detailed information about the range of numbers at each row by just removing the // to uncomment the below shown lines of the code.

//cout << "Row # " << i << " contains the numbers from " << first << " to " << last << " :    ";

Output 1: When you run the provided code as it is,

C++ Floyd's Triangle 2

Output 2: When the line printing out the range is uncommented,

C++ Floyd's Triangle 1

We will highly recommend you to first draw such patterns line by line on a paper before getting into programming them, as it will help you understand the nested structure better.

Keep Learning : )

About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.