Printing star pattern using For Loop in C++


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

The first for loop is responsible for printing the top half of the diamond pattern, starting from the first row and going up to the nth row. The i variable represents the row number. The first inner for loop prints the required number of spaces before printing the asterisks in the left half of the diamond. The second inner for loop prints the asterisks in the left half of the diamond, starting from the second row and going up to the ith row. The third inner for loop prints the asterisks in the right half of the diamond, starting from the first row and going up to the ith row.

The second for loop is responsible for printing the bottom half of the diamond pattern, starting from the first row and going up to the nth row. The i variable represents the row number. The first inner for loop prints the required number of spaces before printing the asterisks in the right half of the diamond. The second inner for loop prints the asterisks in the right half of the diamond, starting from the nth row and going down to the i+1th row. The third inner for loop prints the asterisks in the left half of the diamond, starting from the first row and going up to the (n-i-1)th row

Overall, this program uses three nested for loops to print the diamond pattern. The first two for loops print the top half and bottom half of the diamond pattern, respectively, while the third inner for loop is used twice to print the asterisks in both the left and right halves of the diamond pattern.

Source Code

#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-i);j++)
        {
            cout<<"  ";
        }
        for(j=1;j<i;j++)
        {
            cout<<" *";
        }
        for(j=1;j<=i;j++)
        {
            cout<<" *";
        }
    }
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=i;j++)
        {
            cout<<"  ";
        }
        for(j=n;j>i;j--)
        {
            cout<<" *";
        }
        for(j=1;j<(n-i);j++)
        {
            cout<<" *";
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:5
         *
       * * *
     * * * * *
   * * * * * * *
 * * * * * * * * *
   * * * * * * *
     * * * * *
       * * *
         *


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