C++ program to print mirrored half diamond star pattern

Example

Input :

  • Save

Output :

  • Save

Required Knowledge

Basic C++ programming, if-else, for loop, nested loop

Program to print mirrored half diamond star pattern

/**
     C++ program to print mirrored half diamond star pattern
*/

#include <iostream> 
using namespace std;
int main() 
{
     int i, j, N;
     int star, spaces;
     
     /* Input number of columns to print from user */
     cout<<"Enter number of columns : ";
     cin>>N;
     spaces = N-1;
     star = 1;

     /* Iterate through rows */
     for(i=1; i<N*2; i++)
     {
         /* Print leading spaces */
         for(j=1; j<=spaces; j++)
             cout<<" ";

         /* Print stars */
         for(j=1; j<=star; j++)
             cout<<"*";
   
         /* Move to next line */
         cout<<"\n";

         if(i < N)
         {
             star++;
             spaces--;
         }
         else
         {
             star--;
             spaces++;
         }
     }
     return 0;
 } 
                     Output
Enter number of columns: 5
     *
    **
   ***
  ****
 *****
  ****
   ***
    **
     *
  • Save
Share via
Copy link
Powered by Social Snap