Open In App

C++ Program to Find Factorial of a Large Number Using Recursion

Last Updated : 17 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a large number N, task is to find the factorial of N using recursion.

Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.

Examples:

Input : N = 100
Output : 933262154439441526816992388562667004-907159682643816214685929638952175999-932299156089414639761565182862536979-208272237582511852109168640000000000-00000000000000

Input : N = 50
Output : 3041409320171337804361260816606476884-4377641568960512000000000000

 

Iterative Approach: The iterative approach is discussed in Set 1 of this article. Here, we have discussed the recursive approach.

Recursive Approach: To solve this problem recursively, the algorithm changes in the way that calls the same function recursively and multiplies the result by the number n. Follow the steps below to solve the problem:

  • If n is less than equal to 2, then multiply n by 1 and store the result in a vector.
  • Otherwise, call the function multiply(n, factorialRecursiveAlgorithm(n - 1)) to find the answer.

Below is the implementation of the above approach.

 
 


Output
30414093201713378043612608166064768844377641568960512000000000000


 

Time Complexity: O(n)
Auxiliary Space: O(K), where K is the maximum number of digits in the output


 


Next Article
Practice Tags :

Similar Reads