In this example, I have shared sum triangle from an array 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#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); } |
Program Output
1 2 3 4 5 |
106 47, 59 20, 27, 32 8, 12, 15, 17 3, 5, 7, 8, 9 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.