Posts which you will also like.

Tuesday, August 17, 2010

A Diamond Series(similar to pascal triangle)





A C++ Program to print the following diamond Series:

                          1
                       2 3 2
                    3 4 5 4 3
                 4 5 6 7 6 5 4
              5 6 7 8 9  8 7 6 5
                 4 5 6 7 6 5 4
                    3 4 5 4 3
                       2 3 2
                          1
------------------------------------------------------------------------------------------------------------
#include
#include
void print(int n)
{
     int k=1,l,s=0,sp=20;
     for(int i=1;i<=n;i++)
     {       l=k;
             for(int p=0;p<=sp;p++)
             {
                     cout<<" ";
             }
             for(int j=1;j<=i;j++)
             {
                     cout<<(l%10)<<" ";
                     l++;
             }
            k++;
            sp=sp-2; 
                    if(k>2)
                    {      s=s+2;
                           int m=s;
                           cout<<(s%10)<<" ";
                           while(m>=k)
                           { m--;
                             cout<<(m%10)<<" ";
                           
                            }
                                         
                              
                   
                    }    cout<<"\n";
            }
            k-=1;
            sp=sp+2;
     for(int i=n-1;i>=1;i--)
     {       sp=sp+2;
             for(int p=0;p<=sp;p++)
             {
                     cout<<" ";
             }
           l=--k;
           int f;
             for(int j=1;j<=i;j++)
             {
                     cout<<(l%10)<<" ";
                     f=l;
                     l++;
                  
             }
             for(int j=1;j
             {        f--;
                     cout<<(f%10)<<" ";
                   
             }
             cout<<"\n";    
     }

}

int main()
{
     print(6); //pass parameter n as your wish
     system("pause");
     return 0;
}

No comments:

Post a Comment

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool