TCS-NQT-C-PROGRAM-logical pyramid 6,28,66,120...

 logical pyramid 6,28,66,120...

Identify the logic behind the series

6 28 66 120 190 276….

The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid.

The Pyramid construction rules are as follows

First number in the series should be at the top of the Pyramid
Last N number of the series should be on the bottom-most layer of the Pyramid, with Nth number being the right-most number of this layer.
Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed.

C-Program:

#include<stdio.h>
int main()
{
 int x=6,y=22,n,i,j,k=0;
 scanf("%d",&n);
 if(n>0 && n<=14)
 {
    for(i=1;i<=n;i++)
    {
        for(j=i;j<n;j++)
        {
            printf(" ");
        }
        for(k=1;k<=i;k++)
        {   
             printf("%05d ",x);
             x=x+y;
             y=y+16;
        }
        printf("\n");
     }
  }
 return 0;

}

INPUT : 10

OUTPUT:

         00006 
        00028 00066 
       00120 00190 00276 
      00378 00496 00630 00780 
     00946 01128 01326 01540 01770 
    02016 02278 02556 02850 03160 03486 
   03828 04186 04560 04950 05356 05778 06216 
  06670 07140 07626 08128 08646 09180 09730 10296 
 10878 11476 12090 12720 13366 14028 14706 15400 16110 
16836 17578 18336 19110 19900 20706 21528 22366 23220 24090 

Comments

Popular Posts