# [TCS] CodeVita Problem : Logic Pyramid C-language Program

Problem : Logic Pyramid
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
1.    First number in the series should be at the top of the Pyramid
2.    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.
3.    Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means.
Example

If input is 2, output will be

00006
00028 00066

If input is 3, output will be

00006
00028 00066
00120 00190 00276

Formal input and output specifications are stated below
Input Format:

First line of input will contain number N that corresponds to the width of the bottom-most layer of the Pyramid
Output Format:

The Pyramid constructed out of numbers in the series as per stated construction rules
Constraints:
1.    0 < N <= 14

Pseudo Code:
The series is subset of Hexagonal Number generated using formula: n * ( 2 * n -1 )

If n is only even number then you will get the series as 6, 28, 66, 120, 190, 276, 378,  496, 630, 780, 946, 1128, 1326, 1540 and so on.

1. for i = 0 to i  < N; i++
a. print blank space for (N- i - 1) times
for j = 0 to j <= i ; j++
a. n = ( i + j +1 ) * 2
b. num =  n  * ( 2 * n  - 1 )
c. if number of digit in num < 5
print 0 for (number of digit in num - 5) times
d. print num
e. if (  j < i )
print space

### TCS CodeVita 2016 Round1 Question: Logic Pyramid

C-Language Program (MockVita1/logicpyramid.c)

#include <stdio.h>
#include <stdbool.h>

int next();

int main()
{
int n, i, row;

scanf("%d", &n);

for (row = 0; row < n; row++)
{
for (i = 1; i < n - row; i++)
printf(" ");

for (i = 0; i <= row; i++)
{
printf("%05d", next());

if (i != row)
printf(" ");
}

if (row != n - 1)
printf("\n");
}

return 0;
}

int next()
{
static int lastSeries    = 6;
static int lastSubSeries = 6;

static bool started = false;

if (!started)
{
started = true;
return lastSeries;
}

lastSubSeries += 16;
lastSeries += lastSubSeries;

return lastSeries;
}

Sources:
https://www.programminggeek.in/
https://github.com/

* Ask us, what you want?