C Program to find Sum of Natural Numbers using Recursion

0

Program

#include <stdio.h>
 
int addNumbers(int n);
 
int main()
{
    int num;
     
    printf("Enter a positive integer (Last number): ");
     
    scanf("%d", &num);
     
    printf("Sum = %d",addNumbers(num));
     
    return 0;
}
 
int addNumbers(int n)
{
    if(n != 0)
     
        return n + addNumbers(n-1);
         
    else
     
        return n;
}

Output

Enter a positive integer (Last number): 20
Sum = 210

Leave a Reply

Your email address will not be published. Required fields are marked *