Printing pyramid pattern using For Loop in C++


This C++ program displays a pattern made of asterisks (*). It takes the input value n from the user, which represents the number of rows in the pattern.

The first for loop is responsible for printing the upper half of the pattern, which consists of n rows. The i variable represents the row number. The inner for loop prints the required number of asterisks in each row, which increases by one for each successive row.

The second for loop is responsible for printing the lower half of the pattern, which also consists of n rows. The i variable represents the row number. The inner for loop prints the required number of asterisks in each row, which decreases by one for each successive row. It starts with printing n-1 asterisks in the first row, and ends with printing a single asterisk in the last row.

Overall, this program uses two nested for loops to print the pattern. The first loop prints the upper half of the pattern, and the second loop prints the lower half of the pattern. The inner loop in both cases prints the required number of asterisks in each row.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,n,j;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=i;j++)
        {
            cout<<" *";
        }
    }
    for(i=0;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<(n-i);j++)
        {
            cout<<" *";
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
 *
 * *
 * * *
 * * * *
 * * * * *
 * * * * * *
 * * * * * * *
 * * * * * * * *
 * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * *
 * * * * * * * *
 * * * * * * *
 * * * * * *
 * * * * *
 * * * *
 * * *
 * *
 *



Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples