Sunday 31 January 2016

Cpp Program to print triangle with numbers

This is a simple Cpp Program which prints out a triangle with respect to the input given , here the obtained triangle is filled with numbers.

Triangle pattern with numbers

Download Code

PROGRAM :

#include <iostream>

using namespace std;

int main()
{
    int i, j, n,k;
    cout << "Enter number of lines[height of triangle] : \t";
    cin >> n;
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            cout << " ";
        }
        for(k=1; k<(i*2); k++)
        {
            cout << k;
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT :

Cpp Program - Triangle with numbers