C++ Diamond Pattern

C++ Diamond Pattern


C++ program to print the diamond pattern using '#' symbols. In this program two for loops are used to print the pattern. The first loop prints the upper part to the diamond ( triangle part ). The second for loop prints the lower part of the diamond (an inverted triangle ) . By combining both of the triangles an diamond pattern structure is formed.

PROGRAM CODE:

 #include<iostream>  
 using namespace std;  
  int main()  
  {  
    int i,j,k=1,s=6,l;  
    for(i=0;i<5;i++,k-=2,s-=1)  
    {  
      for(l=s-1;l>=0;l--)  
      cout<<' ';  
      for(j=k;j<0;j++)  
        cout<<"#";  
      cout<<endl;  
   }  
 k = 9;  
 s = 0;  
 for(i=0;i<5;i++,k-=2,s+=1)  
    {  
      for(l=s;l>=0;l--)  
      cout<<' ';  
      for(j=k;j>0;j--)  
         cout<<"#";  
      cout<<endl;  
    }  
  return 0;  
  }  

OUTPUT:

           #
        # # #
     # # # # #
   # # # # # # #
# # # # # # # # #
   # # # # # # #
      # # # # #
        # # #
          # 

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

0 comments :

Post a Comment