I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.
#include <stdio.h>
#define MAX_SIZE 100
// Function declaration
int sum(int arr[], int start, int len);
int main()
{
int arr[MAX_SIZE];
int num, i, sumofarray;
// Inputtin size and elements in array
printf("Enter size of the array: ");
scanf("%d", &num);
printf("Enter elements in the array: ");
for(i=0; i<num; i++)
{
scanf("%d", &arr[i]);
}
sumofarray = sum(arr, 0, num);
printf("Sum of array elements: %d", sumofarray);
return 0;
}
// Recursively finding the sum of elements in an array.
int sum(int arr[], int start, int len)
{
// Recursion base condition
if(start >= len)
return 0;
return (arr[start] + sum(arr, start + 1, len));
}
I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.
Result:
Enter size of the array: 5
Enter elements in the array: 10
20
30
40
50
Sum of array elements: 150
need an explanation for this answer? contact us directly to get an explanation for this answer