C Program to Find the Sum of Natural Numbers using Recursion

@codevarsity
 · 
2.1k views
 · 

A C programA simple C program to find the sum of natural numbers by using a recursive function.

Code

#include <stdio.h>
int addNumbers(int n);

int main()
{
    int num;
    printf("Enter a positive integer: ");
    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: 20
Sum = 210

Comments