Calculating factorial of large numbers in C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The factorial of a number is a fundamental concept in mathematics, represented by the product of all positive integers up to a given number, denoted as n!. While it is straightforward to compute the factorial of small numbers using basic multiplication, calculating the factorial of large numbers in C can introduce significant challenges due to the limitations of standard data types and precision errors. This article explores methods and techniques to compute the factorial of large numbers efficiently in C.
Mathematical Background
Factorial, denoted as n!, is defined for a non-negative integer n as:
Special cases include:
For small values of n, computing n! is feasible with ordinary arithmetic operations. However, as n increases, the value of n! grows exponentially, quickly surpassing the range of standard integer data types like int and long in C, which can manage only a limited range of values.
Challenges in Computing Factorials
Limitations of Standard Data Types
- Integer Overflow: Standard data types (
int,long,unsigned long) can store values only up to their maximum limits. For instance, on a typical 32-bit system, anunsigned intcan store values up to 2^32−1, which is insufficient for computing large factorials because20!alone equals 2,432,902,008,176,640,000. - Precision: Standard floating-point types (
float,double) lack the precision necessary for large integers due to their inherent limitations in representing significant digits accurately.
Performance Considerations
Factorial calculations involve a large number of multiplications, increasing the computational complexity and time, especially for large n.
Techniques for Calculating Large Factorials
Using Arrays for Arbitrary-Precision Arithmetic
One efficient approach to manage the computation of large factorials in C is to use arrays to store individual digits of the number. This method involves performing arithmetic operations manually, digit by digit, as demonstrated below.
Example Implementation
res[]is used to store digits of the factorial result.multiply()handles the digit-wise multiplication ofres[]with an integer, updating the size of the result.- The final factorial is printed by parsing the digits from the array.
Related reading
- Calculating Hamming Weight in O1
- Calculating large factorial time complexity
- Calculating mid in binary search
- Calculating Percentiles on the fly
- Calculating Pearson correlation and significance in Python
- Calculating powa,b mod n
- Calling C/C from Python?
- Can a reference to an element inside an stdmap be invalidated?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.