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:

C++

Tagged in:

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

Share Article:

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO