Skip to content

Instantly share code, notes, and snippets.

@MdKutubuddinSardar
Created August 19, 2018 06:23
Show Gist options
  • Save MdKutubuddinSardar/ac1187992d26800c240a1508f5465670 to your computer and use it in GitHub Desktop.
Save MdKutubuddinSardar/ac1187992d26800c240a1508f5465670 to your computer and use it in GitHub Desktop.
Add n numbers using recursion in C
/*Add n numbers using recursion*/
/* Author : Md Kutubuddin Sardar*/
/*Date : 19.08.2018*/
#include <stdio.h>
long calculateSum(int [], int);
int main()
{
int n, c, array[100];
long result;
printf("Enter the number of integers you want to add : ");
scanf("%d", &n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
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);
}
/* OUTPUT :
Enter the number of integers you want to add : 4
1
4
9
-23
Sum = -9
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment