C Program to calculate sum of array element using recursion

Simple C program to find sum of array elements: If array is {10,10,.40,40,7,3} then output of this program will be 110.
/* Find sum of array elements */
#include<stdio.h>
/* n=sizeof array-1 */
int SumOfElement(int arr[],int n)
{
 if(n==0)
 return arr[0];
 return arr[n]+SumOfElement(arr,n-1);
}
int main()
{
 int arr[6]={10,10,40,40,7,3};
 /* you can enter size or array element */
 /* here 5 is sizeof array-1 */
 int sum=SumOfElement(arr,5);
 printf("Sum = %d",sum);
 return 0;
}
/* End of program */



Popular posts from this blog