Recursion in C
/*Example of recursion to find sum of n natural numbers*/
#include <stdio.h>int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Expected Output:
Enter a positive integer: 6
sum= 21
Tags:
C Program of "Recursion" to add n natural numbers. recursion
natural numbers addition
Recursion in C