Computer Science, asked by karthik515, 1 year ago

Write a c program to find sum of all array elements using recursion.

Answers

Answered by Anonymous
3

/**

* C program to find sum of array elements using recursion

*/

#include <stdio.h>

#define MAX_SIZE 100

/* Function declaration to find sum of array */

int sum(int arr[], int start, int len);

int main()

{

   int arr[MAX_SIZE];

   int N, i, sumofarray;

   

   /* Input size and elements in array  */

   printf("Enter size of the array: ");

   scanf("%d", &N);

   printf("Enter elements in the array: ");

   for(i=0; i<N; i++)

   {

       scanf("%d", &arr[i]);

   }

   

   

   sumofarray = sum(arr, 0, N);

   printf("Sum of array elements: %d", sumofarray);

return 0;

}

/**

* Recursively find 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));

}

Similar questions