What is recursion? Write a program in C to calculate sum of n natural numbers using recursion.

Mumbai University > COMPS > Sem 3 > Data Structures

Marks: 5 M

Year: Dec 2014

1 Answer
  • Recursion is an ability of a function or algorithm to repeatedly call itself until a certain condition is met. Such a condition is called ab base condition.
  • The function which repeatedly calls itself is called recursive function.
  • Recursion is implemented with stacks which help in keeping the function arguments, return addresses and local variables.  

    int add(int n);
    int main()
    int n;
    printf("Enter a positive integer: ");
    printf("Sum = %d",add(n));
    return 0;
    int add(int n)
    return n+add(n-1);  /* recursive call */
Please log in to add an answer.