C Program to find sum of n natural numbers using recursion
Program
#include<stdio.h>
int sum(int);
void main()
{
int n, sum;
printf("Enter the value of n:\t");
scanf("%d", &n);
sum = sum_rec(n);
printf("Sum of %d numbers: %d\n", n, sum);
}
int sum_rec(int num)
{
int res;
if (num == 1)
return 1;
else
res = num + sum_rec(num - 1);
return res;
}
Output
Enter the value of n: 12
Sum of 12 numbers: 78