Printing the pyramid pattern using characters and for loop C++ code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Printing the pyramid pattern using characters and for loop C++ code example

To show: How to construct the pyramid pattern in C++ programming using the for loop

 

// the pyramid of $ using nested for loops

#include <iostream>

// replace any occurrences of VIEW with character $

#define VIEW '$'

using namespace std;

 

int main(void)

{

int i, j;

 

cout<<"Let have a money pyramid!\n"<<endl;

// first for loop, set the rows...

for(i=1; i<=20; i++)

{

// second for loop, set the space...

for(j=1; j<=20-i; j++)

cout<<" ";

 

// third for loop, print the $ characters...

for(j=1; j<=2*i-1; j++)

 

// print character

cout<<VIEW;

 

// go to new line

cout<<"\n";

}

return 0;

}

 

Output example:

 

Let have a money pyramid!
                   $
                  $$$
                 $$$$$
                $$$$$$$
               $$$$$$$$$
              $$$$$$$$$$$
             $$$$$$$$$$$$$
            $$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$
          $$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$
     $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index