[prev] 45 [next]

Exercise 9: Function to compute 1+2+3+...+n recursively

Implement the function sumTo() recursively

int main()
{
   int max;
   print("Enter +ve integer: ");
   scanf("%d", &max);
   printf("Sum 1..%d = %d\n", max, sumTo(max));
   return 0;
}

int sumTo(int n)
{
   if(n == 0){
       return 0;
   }
   return n + sumTo(n-1);
}