
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Factorial of a large number
In computers, variables are stored in memory locations. But the size of the memory location is fixed, so when we try to find the factorial of some greater value like 15! or 20! the factorial value exceeds the memory range and returns wrong results.
For calculation of large numbers, we have to use an array to store results. In each element of the array, is storing different digits of the result. But here we cannot multiply some number with the array directly, we have to perform manual multiplication process for all digits of the result array.
Input and Output
Input: A big number: 50 Output: Factorial of given number is: 30414093201713378043612608166064768844377641568960512000000000000
Algorithm
multiply(x, multiplicand)
Input: The number x, and the large multiplicand as an array.
Output: Result after multiplication.
Begin carry := 0 for all digits i of multiplicand, do prod := i*x+carry i := prod mod 10 carry := prod / 10 done while carry ≠ 0, do insert (carry mod 10) at the end of multiplicand array carry := carry/10 done End
factorial(n)
Input: The number n.
Output: Find factorial of n.
Begin define result array. insert 1 in the result for i := 2 to n, do multiply(i, result) done reverse the result return result End
Example
#include<iostream> #include<vector> #include<algorithm> using namespace std; void multiply(int x, vector<int>&multiplicand) { //multiply multiplicand with x int carry = 0; // Initialize carry to 0 vector<int>::iterator i; for (i=multiplicand.begin(); i!=multiplicand.end(); i++) { //multiply x with all digit of multiplicand int prod = (*i) * x + carry; *i = prod % 10; //put only the last digit of product carry = prod/10; //add remaining part with carry } while (carry) { //when carry is present multiplicand.push_back(carry%10); carry = carry/10; } } void factorial(int n) { vector<int> result; result.push_back(1); //at first store 1 as result for (int i=2; i<=n; i++) multiply(i, result); //multiply numbers 1*2*3*......*n cout << "Factorial of given number is: "<<endl; reverse(result.begin(), result.end()); vector<int>::iterator it; //reverse the order of result for(it = result.begin(); it != result.end(); it++) cout << *it; } int main() { factorial(50); }
Output
Factorial of given number is: 30414093201713378043612608166064768844377641568960512000000000000
- Related Articles
- Python program to find factorial of a large number
- Factorial of Large Number Using boost multiprecision Library
- C++ Program to Find Factorial of Large Numbers
- Python Program for factorial of a number
- Returning number of digits in factorial of a number in JavaScript
- Java Program to Find Factorial of a number
- Swift Program to Find Factorial of a number
- Kotlin Program to Find Factorial of a number
- Program for factorial of a number in C program
- Function to compute factorial of a number in JavaScript
- 8085 program to find the factorial of a number
- 8086 program to find the factorial of a number
- First digit in factorial of a number in C++
- How to Find the Factorial of a Number using Python?
- C++ program to Calculate Factorial of a Number Using Recursion

Advertisements