Friday, 27 May 2016

Write a program to generate the following pyramid of digits using nested loop: 


Solution: 

#include<iostream.h> 
#include<conio.h> 
void main()
{
     int mid; 
    clrscr(); 
    for(int i=1; i<=10; i++)
    {
           cout<<" "; 
           mid =(2*i)-1;
           for(int j=1; j<=(10-i); j++)
              cout<<" "; 
           for(j=i; j<=mid; j++)
              cout<<(j%10); 
           for(j =(mid-1); j>=i; j--)
             cout<<(j%10);
          cout<<endl;
     }
      getch();
}


No comments:

Post a Comment