# Factorial using Recursion in C++

Factorial is a fundamental mathematical concept that finds its significance in various algorithms and problem-solving techniques. In this article, we will write a program to calculate factorial in C++ using recursion.

Contents

## What is Factorial ?

Factorial, denoted by the exclamation mark ‘!’, is a mathmetical operation that calculates the product of all positive integers from 1 to a given number.

For example factorial of 6 is denoted as 6! and calculated as 6*5*4*3*2*1 = 720.

## Factorial Using Recursion in C++

The recursive method for calculating factorial in C++ involves defining a function that calls itself again and again with a smaller value (function argument) until it reaches the base condition of factorial i.e. n=0 or n=1.

``````#include <iostream>

unsigned long long factorialRecursive(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorialRecursive(n - 1);
}

int main() {
int num;
std::cout << "Enter a positive integer: ";
std::cin >> num;
std::cout << "Factorial of " << num << " is: " << factorialRecursive(num) << std::endl;
return 0;
}
``````

You can check this tutorial for calculating factorial in C++ by using the iterative approach Factorial in C++

Categories C++