C Program to Add First n Natural Numbers Using Recursion

Here’s a simple C program which adds first n natural numbers.

#include<stdio.h>
int fun(int);
int main()
{
    int k,a;
    printf("Enter a: ");
    scanf("%d",&a);
    k=fun(a);
    printf("Sum of First %d Numbers is %d",a, k);
    return 0;
}
int fun(int b)
{
    int s=0;
    if (b==0)
        return s;
    else
        s=b+fun(b-1);
    return s;
}

Output:

Enter a: 5
Sum of First 5 Numbers is 15

[print-me]

Add a Comment