Programming Examples
C program to evaluate the given series using recursive function sum
Write a complete program to evaluate the given series using recursive function sum( ). Here n is user dependent.
1 + 2 + 3 +…+ n
Solution
#include <stdio.h>
int sum(int);
int main()
{
int num,ans;
printf("Enter the Number of Term in Series : ");
scanf("%d",&num);
ans=sum(num);
printf("Sum of Series : %d",ans);
return 0;
}
int sum(int n)
{
if(n==1)
{
return 1;
}
else
{
return n+sum(n-1);
}
}
Output
Enter the Number of Term in Series : 5
Sum of Series : 15