C Program of "Recursion" to add n natural numbers.

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



Post a Comment

Previous Post Next Post