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

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz