C program to calculate sum of numbers 1 to N using recursion

C program to calculate sum of numbers 1 to N using recursion:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

int calSum(int);
main()
{
   int i,num;
   int result;

   printf("Enter end number :");
   scanf("%d",&num);

   result=calSum(num);
   printf("\nSum of Number From 1 to %d : %d",num,result);
}

int calSum(int num)
{
   int res;
   if(num==1)
    {
      return(1);
    }
    else
    {
      res=num+calSum(num-1);
    }
   return(res);
}

OUTPUT:

Enter end number :5

Sum of Number From 1 to 5 : 15

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.