C++ Program To Print Diamond Pattern

 

C++ Program To Print Diamond Pattern, Diamond Pattern in c++, pattern , pattern problem

Program:

#include <iostream>
using namespace std;

int main()
{
    int n;
    cout<<"Enter number of rows: ";
    cin>>n;
    
    //loop for upper half
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++) //loop to print spaces before printing *
        cout<<"  ";
        
        for(int j=1;j<=i;j++)   //loop to print * left side
        cout<<"* ";
        
        for(int j=1;j<=i-1;j++) //loop to print * right side
        cout<<"* ";
        
        cout<<endl; //change line after printing every row
    }
    
    //loop for lower half
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=n-i;j++)
        cout<<"  ";
        
        for(int j=1;j<=i;j++)
        cout<<"* ";
        
        for(int j=1;j<=i-1;j++)
        cout<<"* ";
        
        cout<<endl;
    }
    return 0;
}

Output:

    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *
C++ Program To Print Diamond Pattern C++ Program To Print Diamond Pattern Reviewed by Beast Coder on February 14, 2021 Rating: 5

No comments:

Powered by Blogger.