Print pyramid triangle in C++

  • To print pyramid triangle in C++, we have use two for loop, the outer for loop and the inner for loop.
  • The outer for loop is responsible for rows and the inner for loop is responsible for column.
  • C++ programs to print different types of patterns using stars(*), numbers, and characters or alphabets.
  • This C++ program prints the full pyramid using stars(*).

Sample Code in C++

#include <iostream>
using namespace std;
int main()
{
int j, rows;
cout <<"Enter number of rows: ";
cin >> rows;
for(int i = 1, k = 0; i <= rows; ++i, k = 0)
{
for(j = 1; j <= rows-i; ++j)
{
cout <<" ";
}
while(k != 2*i-1)
{
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}

Output

Enter number of rows : 7   
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,