Recursive version of the sum of the first n integers in C

#include <stdio.h>
 
 int sum(int);
 
 int main()
 {
    int s,n;
 
    printf("Enter the value of n: ");
    scanf("%d",&n);
    s=sum(n);
    printf("nSum of the first %d integers is %d.n",n,s);
    return 0;
 }
 
 int sum(int n)
 {
    if(n <= 1)
        return n;
    else
        return (n + sum(n-1));
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.