C++ Program To Print Hollow Diamond Pattern

C++ Program To Print Hollow Diamond Pattern, 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  
         {  
               if(j==1 )      // we have to print * only at boundaries
               cout<<"* ";  
         
               else  
               cout<<" ";  
         }  
         for(int j=1;j<=i-1;j++)     // loop to print * right side  
         {  
               if( j==i-1 )  
               cout<<"* ";  
         
               else  
               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++)     // loop to print spaces before printing *  
         cout<<" ";  
       
         for(int j=1;j<=i;j++)      // loop to print * left side  
         {  
               if(j==1 )  
               cout<<"* ";  
         
               else  
               cout<<" ";  
         }  
         for(int j=1;j<=i-1;j++)     // loop to print * right side  
         {  
               if( j==i-1 )  
               cout<<"* ";  
         
              else  
               cout<<" ";  
         }  
         cout<<endl;     // change line after printing every row  
   }  
   return 0;  
 }  

 Output:

    *
   * *
  *   *
 *     *
*       *
*       *
 *     *
  *   *
   * *
    *

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

No comments:

Powered by Blogger.