Printing diamond pattern outline using For Loop in C++


This is a C++ program that uses loops to print a diamond pattern of stars. The pattern is printed in two parts - first, the upper half of the diamond is printed, and then the lower half is printed. Here is an explanation of how the program works:

  • The program first declares three integer variables - i, j, and n.
  • The user is prompted to enter a value for n.
  • The program enters a loop that iterates n times. This loop is responsible for printing the upper half of the diamond.
  • Within the first loop, there is another loop that prints spaces before the stars. The number of spaces printed is equal to n-i.
  • Then, there is another loop that prints the first half of the stars. If j is equal to 1, a star is printed, otherwise, spaces are printed.
  • Next, there is another loop that prints the second half of the stars. If j is equal to i, a star is printed, otherwise, spaces are printed.
  • After the first loop completes, there is another loop that iterates n times. This loop is responsible for printing the lower half of the diamond.
  • Within the second loop, there is another loop that prints spaces before the stars. The number of spaces printed is equal to i.
  • Then, there is another loop that prints the first half of the stars. If j is equal to n, a star is printed, otherwise, spaces are printed.
  • Next, there is another loop that prints the second half of the stars. If j is equal to n-i+1, a star is printed, otherwise, spaces are printed.
  • The program then exits the second loop and the main() function, returning 0.
  • The diamond pattern is printed to the console.

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++)
        {
            if(j==1)
            {
                cout<<" *";
            }
            else
            {
                cout<<"  ";
            }
        }
        for(j=1;j<=i;j++)
        {
            if(j==i)
            {
                cout<<" *";
            }
            else
            {
                cout<<"  ";
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=i;j++)
        {
            cout<<"  ";
        }
        for(j=n;j>i;j--)
        {
            if(j==n)
            {
            cout<<" *";
            }
            else
            {
                cout<<"  ";
            }
        }
        for(j=2;j<=(n-i);j++)
        {
            if(j==n-i)
            {
            cout<<" *";
            }
            else
            {
                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