Dremendo Tag Line

Print pyramid star pattern using for loop in C++

for Loop - Question 18

In this question, we will see how to print the pyramid star pattern in C++ programming using for loop. To know more about for loop click on the for loop lesson.

Q18) Write a program in C++ to print the pyramid star pattern using for loop.

    *
   * *
  * * *
 * * * *
* * * * *

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int r,c;

    for(r=1; r<=5; r++)
    {
        // printing the space
        for(c=4; c>=r; c--)
        {
            cout<<" ";
        }

        // printing the star
        for(c=1; c<=r; c++)
        {
            cout<<"* ";		// print the star with a space, it will make a triangle
        }
        cout<<endl;
    }
    return 0;
}

Output

    *
   * *
  * * *
 * * * *
* * * * *
video-poster