
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Factorial of Large Numbers
A factorial of a number defines the non-negative integer say n that calculate the product of a number by every positive integer until it reaches 1. The symbol of factorial is (!).
Mathematically, it is represented by:
n! = n x (n-1) x (n-2) x ... x 1
For eg. factorial of an integer 30! = 265252859812191058636308480000000.
30! = 30x29x28x27x26x25x24x23x22x21x20x19x18x17x16x15x14x13x12x11x10x9x8x7x6x5x4x3x2x1
A non-negative integer is defined by any whole number that is 0 or positive (not a fraction or decimal).
What is Large Number?
In context of calculating the factorial number, the large number denotes the n value that calculate the factorial of a number using formula.
Here, we have the following list to solve the factorial of a large number:
Factorial of Large Number Using Recursion
The recursion means function calling to function itself. Here, the custom function factorial accepts the parameter n set to the base case using the if statement to reach 1 or 0. Then works for small integers up to 20!. So, this finds the factorial of a large number.
Example
In this example, we find the factorial of large number using recursion.
#include <iostream> using namespace std; unsigned long long factorial(int n) { if (n == 0 || n == 1) return 1; return n * factorial(n - 1); } int main() { int n = 20; cout << "Factorial of " << n << " is: " << factorial(n) << endl; return 0; }
Output
The above code produces the following result:
Factorial of 20 is: 2432902008176640000
Using Vector for Large Integer
To calculate the factorial of aa number, use vector and follow the basic algorithm to handle carry and display digit in reverse order to get the expected result.
Algorithm
Here, we are providing the list to steps to find the factorial for large number as follows:
- Step 1: Store the factorial of a number in a vector, starting with 1 as the initial result.
- Step 2: Now, for each number i from 2 to n, product the current result with i.
- Step 3: Next, the multiply() function updates the vector with new numbers and handles like basic arithmetic.
- Step 4: The reverse numbers are displayed to get the factorial value.
Example
In this example, we shows the usage of vector to get the factorial of a large number.
#include <iostream> #include <vector> using namespace std; // Function to product a large number void multiply(vector<int>& result, int x) { int carry = 0; for (int i = 0; i < result.size(); i++) { int prod = result[i] * x + carry; result[i] = prod % 10; carry = prod / 10; } // Add remaining carry digits while (carry) { result.push_back(carry % 10); carry /= 10; } } // Function to calculate factorial void factorial(int n) { vector<int> result; result.push_back(1); // 0! = 1 for (int i = 2; i <= n; i++) { multiply(result, i); } // Since digits are stored in reverse order, display in reverse for (int i = result.size() - 1; i >= 0; i--) { cout << result[i]; } cout << endl; } int main() { int n = 40; cout << n << "! = "; factorial(n); return 0; }
Output
The above code produces the following result:
40! = 815915283247897734345611269596115894272000000000
Using <string> & <algorithm>
In this approach, use the header like <string> and <algorithm> that has functions like reverse(), begin(), and end() which reverse the digit say(num) as per the order of elements. Then use the loops for calculating carry and start the main() function to pass the factorial input to get its value.
Example
In this example, we use the header functions of both string and algorithm to calculate the factorial of a large number.
#include <iostream> #include <string> #include <algorithm> using namespace std; // Function to multiply a large number string multiply(string num, int x) { int carry = 0; string result = ""; // Start from least significant number reverse(num.begin(), num.end()); for (char digit : num) { int prod = (digit - '0') * x + carry; result += (prod % 10) + '0'; carry = prod / 10; } // Add remaining carry while (carry) { result += (carry % 10) + '0'; carry /= 10; } // Reverse to normal order reverse(result.begin(), result.end()); return result; } // Function to calculate factorial using string string factorial(int n) { string result = "1"; for (int i = 2; i <= n; i++) { result = multiply(result, i); } return result; } int main() { int n = 100; string fact = factorial(n); cout << n << "! = " << fact << endl; return 0; }
Output
The above code produces the following result:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000