A program in C++ to calculate the factorial of a given number by using recursion.

To understand the above C++ program to calculate the factorial of a number using recursion, we need to understand what is recursion first. Recursion is a method in C++ which calls itself directly or indirectly until a suitable condition is met. In this method, we repeatedly call the function within the same function, and it has a base case and a recursive condition.

#include<iostream>
using namespace std;

int factorial(int num);

int main() {

  int num;

  cout << "Enter a positive integer: ";
  cin >> num;

  cout << "Factorial of " << num << " is : " << factorial(num);

  return 0;
}

int factorial(int num) {
  if(num > 1)
    return num * factorial(num - 1);
  else
    return 1;
}
Sample Output:
Enter a positive integer: 12
Factorial of 12 is : 479001600