0
5.7kviews
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
0
1.3kviews
  • 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.  

    #include<stdio.h>
    int add(int n);
    int main()
    {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    printf("Sum = %d",add(n));
    return 0;
    }
    
    int add(int n)
    {
    if(n!=0)
    return n+add(n-1);  /* recursive call */
    }
    
Please log in to add an answer.