Given a number N, the task is to find the factorial of it. A factorial is the product of all the natural numbers less than or equal to the given number N.
Examples
Input: N = 5
Output: 120
Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120Input: N = 0
Output: 1
Explanation: 0! = 1 by definition
Find Factorial Using a Loop
The simplest way to find the factorial of a number N is by using a loop to repeatedly multiply the numbers from 1 to N and store the result in a variable.
#include <stdio.h>
unsigned int factorial(unsigned int N) {
int fact = 1, i;
// Loop from 1 to N to get the factorial
for (i = 1; i <= N; i++) {
fact *= i;
}
return fact;
}
int main() {
int N = 5;
int fact = factorial(N);
printf("Factorial of %d is %d", N, fact);
return 0;
}
Output
Factorial of 5 is 120
Time Complexity: O(N)
Space Complexity: O(1)
Using Recursion
The idea is to use the concept of recursion. We create a recursive function with the argument N which will progressively decrement by 1 till it reaches 0. In each recursive call, we return the function call for decremented N after multiplying it with current N. At the end, we will be left with the factorial of N.
#include <stdio.h>
unsigned int factorial(unsigned int n) {
// Base Case:
if (n == 1) {
return 1;
}
// Multiplying the current N with the previous product
// of Ns
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Output
Factorial of 5 is 120
Time Complexity: O(N)
Auxiliary Space: O(N), due to the recursive call stack.
Using Arrays (for Large Numbers)
Factorial may get very large which can be troublesome because the largest number the C++ is 18,446,744,073,709,551,615. We can solve this using strings to store the numbers and perform the multiplication.
- Convert the given number N to the string num.
- Multiply the num with every number from 1 to N with the current value stored in the array fact. Refer this article to know about it.
- Print the digits of the result array in reverse order.
#include <stdio.h>
#include <string.h>
// Function to multiply a string (representing a number)
// by an integer
void multiplyString(char num[], int factor) {
int len = strlen(num);
int carry = 0;
for (int i = len - 1; i >= 0; i--) {
int digit = num[i] - '0';
int product = digit * factor + carry;
num[i] = (product % 10) + '0';
carry = product / 10;
}
// Handling the carry by adding digits to the front
// of the number
while (carry) {
for (int i = strlen(num); i >= 0; i--) {
num[i + 1] = num[i];
}
num[0] = (carry % 10) + '0';
carry /= 10;
}
}
// Function to find factorial using a string
void factorialString(int N) {
// Use a large enough buffer
char fact[1000];
// Initialize result as "1"
strcpy(fact, "1");
for (int i = 2; i <= N; i++) {
// Multiply the string in each iteration
multiplyString(fact, i);
}
printf("Factorial of %d is %s", N, fact);
}
int main() {
int N = 5;
factorialString(N);
return 0;
}
Output
Factorial of 5 is 120
Time Complexity: O(N log2N)
Auxiliary Space: O(logN)