codescracker
c++

C++ Program to Print Floyd's Triangle



« Previous Program Next Program »

In this article, you will learn and get code to print Floyd's Triangle of default and given size (by user), using C++ program. Here are the list of programs:

Before creating these programs, let's first understand about, how a Floyd's Triangle looks like.

What is Floyd's Triangle ?

Floyd's triangle is a right angled-triangle formed by natural numbers as shown here:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Print Floyd's Triangle

The program given below prints Floyd's triangle of 5 lines or rows. The question is, write a program in C++ to print Floyd's Triangle. Here is its answer:

// C++ Program to Print Floyd's Triangle
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, num=1;
    for(i=0; i<5; i++)
    {
        for(j=0; j<=i; j++)
        {
            cout<<num<<" ";
            num++;
        }
        cout<<endl;
    }
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample output:

C++ program print floyd triangle

Print Floyd's Triangle of Given Size

This program allows user to define the size of Floyd's triangle. That is, upto how many lines or rows, Floyd's triangle has to expand. Let's have a look at the program:

// C++ Print Floyd's Triangle of Given Size
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int i, j, num=1, rowSize;
    cout<<"Enter Row Size: ";
    cin>>rowSize;
    cout<<"\nFloyd's Triangle of "<<rowSize<<" Lines:\n";
    for(i=0; i<rowSize; i++)
    {
        for(j=0; j<=i; j++)
        {
            cout<<num<<" ";
            num++;
        }
        cout<<endl;
    }
    return 0;
}

Here is its sample run. This is the initial output:

print floyd triangle c++

Now supply the input say 10 to print Floyd's Triangle of 10 lines as shown in the snapshot given below:

c++ print floyd triangle

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker