What is recursion in C ?

devquora
devquora

Posted On: Feb 22, 2018

 

Recursion: A function, which calls itself, recursion is simple to write in program but takes more memory space and time to execute.

Advantages of using recursion:-

  • Avoid unnecessary calling of functions
  • Substitute for iteration
  • Useful when applying the same solution

Factorial program in c using Recursion

#include 

int factorial(unsigned int i) {

   if(i <= 1) {
      return 1;
   }
   return i * factorial(i - 1);
}

int  main() {
   int i = 15;
   printf("Factorial of %d is %d\n", i, factorial(i));
   return 0;
}

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    C programming interview questions

    What are static variables in C ?

    C defines another class of variables called static variables. Static variables are of two types:Static variables tha..

    C programming interview questions

    What is a scope resolution operator in C ?

    The scope resolution operator (::) is useful in C programming when both global and local variables have the same name,..

    C programming interview questions

    What is register variable in C language ?

    C language allows the use of the prefix register in primitive variable declarations. Such variables are called register..