Printing boat pattern using For Loop in C++


This C++ program displays a pattern made of asterisks (*). It takes the input value a 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 a rows. The i variable represents the row number. The inner for loop prints the required number of spaces in each row, which is equal to a. The next 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 a rows. The i variable represents the row number. The first inner for loop prints the required number of spaces in each row, which starts with a single space for the first row and increases by one for each successive row. The next inner for loop prints the required number of asterisks in each row, which starts with a-i asterisks in the first row and decreases by one for each successive row. The third inner for loop prints a asterisks in each row. The fourth inner for loop prints the required number of asterisks in each row, which is equal to (a-i) asterisks for the first row and increases by one for each successive row.

Overall, this program uses four 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 loops in both cases print the required number of spaces and asterisks in each row.

Source Code

#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<=i;n++)
        {
            cout<<" *";
        }
    }
    for(i=1;i<=a;i++)
    {
        cout<<"\n";
        for(n=1;n<=i;n++)
        {
            cout<<"  ";
        }
        for(n=1;n<=(a-i);n++)
        {
            cout<<" *";
        }
        for(n=1;n<=a;n++)
        {
            cout<<" *";
        }
        for(n=1;n<=(a-i);n++)
        {
            cout<<" *";
        }

    }
}

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