write a c program to find the sum of natural numbers using recursion

Write a c program to find the sum of natural numbers using recursion. A recursive function is a function that calls itself. Recursion is the name for this strategy. Recursion is a problem-solving technique in which the outcome is determined by the solutions to smaller instances of the same problem. Recursion is supported by most computer programming languages by allowing a function to call itself from within its own code.

#include <stdio.h>
int sum(int n);
int main() 
{
int num, total;
printf("Enter the num");
scanf("%d", &num);
total = sum(num);
printf("sum = %d", total);
return 0;
}
int sum(int n) 
{
if (n != 0)
return n + sum(n-1); 
else
return n;
}

 

Output:

Enter the num 45

sum = 1035