+92 332 4229 857 99ProjectIdeas@Gmail.com

Diamond (C++)


Diamond
drawDiamond() draws a diamond and it takes 2 parameters:
noOfLines takes the number of lines.
ch takes the character to be displayed in the diamond.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"

using namespace std;

void drawDiamond(int noOfLines,char ch)
{
       if(noOfLines%2!=0)
       {
              for(int i=0;i<noOfLines;i=i+2)
              {
                     for(int j=0;j<(noOfLines-2)-i;j=j+2)
                           cout<<" ";
      
                     for(int k=0;k<i+1;k++)
                           cout<<ch;
              cout<<endl;
              }

              for(int l=0;l<noOfLines-2;l=l+2)
              {
                     for(int m=0;m<l+1;m=m+2)
                           cout<<" ";
    
                     for(int n=0;n<(noOfLines-2)-l;n=n+1)
                           cout<<ch;
        cout<<endl;  
              }
       }
       else
              cout<<"Diamond shape is not possible on even number of lines"<<endl;
}

int main()
{
       drawDiamond(27,'*');cout<<endl;
      
       _getche();
       return 0;
}

Output
             *
            ***
           *****
          *******
         *********
        ***********
       *************
      ***************
     *****************
    *******************
   *********************
  ***********************
 *************************
***************************
 *************************
  ***********************
   *********************
    *******************
     *****************
      ***************
       *************
        ***********
         *********
          *******
           *****
            ***
             *

0 comments: