You are on page 1of 3

S2 BTech

C Programming

Code-Chef Submission

Name: ANANDHA KRISHNAN. R

Roll No: AM.EN.U4ECE17109

QUESTION: 4: SUMTRIAN

BEGINNER

Let's consider a triangle of numbers in which a number appears in


the first line, two numbers appear in the second line, three in the
third line, etc. Develop a program which will compute the largest of
the sums of numbers that appear on the paths starting from the top
towards the base, so that:

on each path the next number is located on the row below, more
precisely either directly below or below and one place to the right;

the number of rows is strictly positive, but less than 100

all numbers are positive integers between 0 and 99.

//ANANDHA KRISHNAN.R
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,i,j,y,x;
scanf("%d",&n);
int a[n][n];

for(i=0;i<n;i++)
for(j=0;j<i+1;j++)
scanf("%d",&a[i][j]);

for(i=n-2;i>=0;i--)
for(j=0;j<n-1;j++)
{
x=a[i][j]+a[i+1][j];
y=a[i][j]+a[i+1][j+1];
a[i][j]=x>y?x:y;
}

printf("%d\n",a[0][0]);
}
return 0;
}
APPROACH:
First enter the number of test cases to be executed ,then enter the
number of lines of the triangle .Then enter the values in each line of
the triangle(which will be stored as an array).Then the program will
check for the greatest sum from the second last line and last line and
saves those values the array value of second last line. Then it
compares the third last line and second last line and so on. And
finally will print the value of a[0][0] , which will have the largest sum

You might also like