- Related Questions & Answers
- Average numbers in array in C Programming
- Add minimum number to an array so that the sum becomes even in C programming
- Arithmetic Mean in C programming
- Concatenate a string given number of times in C++ programming
- Write a program to Delete a Tree in C programming
- Write a function that returns 2 for input 1 and returns 1 for 2 in C programming
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Surface Area and Volume of Hexagonal Prism in C programming
- Superperfect Number in C programming
- Super Prime in c programming
- Sums of ASCII values of each word in a sentence in c programming
- C Programming for sum of the series 0.6, 0.06, 0.006, 0.0006, …to n terms
- C Programming for Sum of sequence 2, 22, 222, ………
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Sum of the first N terms of the series 5,12, 23, 38…. in C Programming

The sum triangle from an array is a triangle that is made by decreasing the number of elements of the array one by one and the new array that is formed is with integers that are the sum of adjacent integers of the existing array. This procedure continues until only one element remains in the array.

Let's take an example to explain the content better,

Array = [3,5,7,8,9]

**Output**

[106] [47,59] [20,27,32] [8,12,15,17] [3,5,7,8,9]

**Explanation**

For the first array : ( 3 + 5 = 8), ( 5 + 7 = 12), ( 7 + 8 = 15),( 8 + 9 = 17) For the second array: 8 + 12 = 20 , 12 + 15 = 27 , 15 + 17 = 32 For the third array: 20 + 27 = 47 , 27 + 32 = 59 For the final array: 47 + 59 = 106

The code runs as it is shown in the example explanation. So for this we need a recursive function that will call itself for every array.

#include<stdio.h> void printTriangle(int arr[] , int n) { if (n < 1) { return; } int temp[n - 1]; for (int i = 0; i < n - 1; i++) { int x = arr[i] + arr[i + 1]; temp[i] = x; } printTriangle(temp, n - 1); for (int i = 0; i < n ; i++) { if(i == n - 1) printf("%d ",arr[i]); else printf("%d, ",arr[i]); } printf("\n"); } int main() { int arr[] = { 3,5,7,8,9}; int n = sizeof(arr) / sizeof(arr[0]); printTriangle(arr, n); }

106 47, 59 20, 27, 32 8, 12, 15, 17 3, 5, 7, 8, 9

Advertisements