C++ Recursion

Process in which a function repeatedly calls itself. It helps in making program code compact.

Program to calculate factorial of number using recursion.



#include<iostream.h>
#include<conio.h>
int factorial(int n)
{
    if(n==1||n==0)
    {
       return 1;
    }
    else{
       return n*factorial(n-1);
    }
}
void main()
{
      clrscr();
      int f,m;
      cout<<"Enter number = ";
      cin>>m;
      f=factorial(m);
      cout<<"\nFactorial="<<f;
      getch();
}


Output


Enter number = 4
Factorial=24

Back To Top