Printing flag 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 top portion of the pattern, which consists of n rows. The i variable represents the row number. The first inner for loop prints the required number of spaces before printing the asterisks in each row. The second inner for loop prints n+5 asterisks in each row, without any spaces in between.

The second for loop is responsible for printing a single asterisk at the center of the pattern. It simply prints n spaces before printing the asterisk.

The third for loop is responsible for printing the bottom portion of the pattern, which also consists of n rows. The i variable represents the row number. The first inner for loop prints two spaces at the beginning of each row. The second inner for loop prints the required number of spaces before printing the asterisks in the left half of the pattern. The third inner for loop prints the asterisks in the left half of the pattern. The fourth inner for loop prints the asterisks in the right half of the pattern.

Overall, this program uses four nested for loops to print the pattern. The first loop prints the top portion of the pattern, the second loop prints a single asterisk, and the third loop prints the bottom portion of the pattern. The fourth inner for loop is used twice to print the asterisks in both the left and right halves of the pattern.

Source Code Example : 1

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

Output

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

Source Code Example : 2

#include<iostream>
using namespace std;
int main()
{
    int i,n,a;
    cout<<"\nEnter the value:";
    cin>>a;
    for(i=1;i<=a;i++)
    {
        cout<<"\n";
        for(n=1;n<=a;n++)
        {
            cout<<"  ";
        }
        for(n=1;n<=a+3;n++)
        {
            cout<<" *";
        }
    }
    for(i=1;i<=a;i++)
    {
        cout<<"\n";
        for(n=1;n<=a;n++)
        {
            cout<<"  ";
        }
        cout<<" *";
    }
    for(i=1;i<=a;i++)
    {
        cout<<"\n";
        for(n=1;n<=(a-i);n++)
        {
            cout<<"  ";
        }
        for(n=1;n<=i;n++)
        {
            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