C program to find sum of n numbers using recursion

#include< stdio.h >
#include< conio.h >
void main()
{
         int n,sum;
         int recursion (int n);
         clrscr();
         printf("Enter a number:");
         scanf("%d",&n);
         S=recursion(n);
         printf("Sum of numbers from 1 to %d is %d",n,S);
         getch();
}
int recursion (int n)
{
        if(n==1)
                 return 1;
        else
                 return (n + recursion (n-1));
}

OUTPUT :

Enter a number : 5
Sum of numbers from 1 to 5 is 15

No comments :

Post a Comment