C Program to Add n Numbers Using Recursion

0
848

C Program to Add n Numbers Using Recursion

#include <stdio.h>
 
long calculateSum(int [], int);
 
int main()
{
   int n, i, array[100];
   long result;
 
   scanf("%d", &n);
 
   for (i = 0; i < n; i++)
      scanf("%d", &array[i]);
 
   result = calculateSum(array, n);
 
   printf("Sum = %ld\n", result);
 
   return 0;
}
 
long calculateSum(int a[], int n) {
   static long sum = 0;
 
   if (n == 0)
      return sum;
 
   sum = sum + a[n-1];
 
   return calculateSum(a, --n);
}

Leave a Reply