Printing Pyramid in C++

C++Server Side ProgrammingProgramming

This article yields a “pyramid-like structure” as an output using the C++ programming code. In which the pyramid height and space are being determined by traversing double for loop constructs as following;

Example

 Live Demo

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

Output

After compilation of the above code, the pyramid will be printed looks like as.

         *
       * * *
     * * * * *
   * * * * * * *
 * * * * * * * * *
* * * * * * * * * * *
raja
Published on 23-Dec-2019 10:58:19
Advertisements