Program for factorial of a number
Last Updated :
30 Jul, 2024
Given the number N (N >=0), print its factorial.
Factorial of a non-negative integer is the multiplication of all positive integers smaller than or equal to N. It is represented by a number and a ”!” mark at the end and is widely used in permutations and combinations to calculate the total possible outcomes.
Examples:
Input: 5
Output: 120
Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120
Input: 4
Output: 24
Explanation: 4! = 4 * 3 * 2 * 1 = 24
[Naive Approach] Using recursion – O(N) Time and O(N) Space
Intuition: Let’s try to observe some pattern:
N! = N * (N – 1) * (N – 2) …. 2 * 1
(N – 1)! = (N – 1) * (N – 2) … 2 * 1
From the above two equations, we can say that N! = N * (N – 1)!
Approach:
Since the problem can be broken down into The idea is to define a recursive function, say factorial(N) to calculate the factorial of number n. According to the value of N, we can have two cases:
- Base Case: N = 0 or N = 1, then factorial(N) = 1.
- Recurrence Relation: N > 1, then factorial(N) = N * factorial(N – 1).
Below is the implementation of the above approach:
C++
// C++ program to find factorial of given number
#include <iostream>
using namespace std;
// Function to find factorial
// of given number
unsigned int factorial(unsigned int n)
{
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
// Driver code
int main()
{
int num = 5;
cout << "Factorial of "
<< num << " is " << factorial(num) << endl;
return 0;
}
C
// C program to find factorial of given number
#include <stdio.h>
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Java
// Java program to find factorial of given number
class Test {
// Method to find factorial of the given number
static long factorial(int n)
{
long res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println(
"Factorial of " + num
+ " is " + factorial(5));
}
}
Python
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
# Driver Code
num = 5;
print(f"Factorial of {num} is {factorial(num)}")
C#
// C# program to find factorial
// of given number
using System;
class Test {
// method to find factorial
// of given number
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver method
public static void Main()
{
int num = 5;
Console.WriteLine("Factorial of "
+ num + " is " + factorial(5));
}
}
JavaScript
// Javascript to find factorial
// of given number
// function to find factorial
// of given number
function factorial(n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
// Driver Code
let num = 5;
console.log("Factorial of " + num + " is " + factorial(num));
PHP
<?php
// PHP program to find factorial
// of given number
// function to find factorial
// of given number
function factorial($n)
{
if ($n == 0)
return 1;
return $n * factorial($n - 1);
}
// Driver Code
$num = 5;
echo "Factorial of ", $num, " is ", factorial($num);
?>
OutputFactorial of 5 is 120
Time Complexity: O(n), since the function is being called n times
Auxiliary Space: O(n), In the worst case, the recursion stack space would be full with all the function calls waiting to get completed and that would make it an O(n) recursion stack space.
[Expected Approach] Using loops – O(N) Time and O(1) Space
Factorial can also be calculated iteratively as recursion can be costly for large numbers. Since the factorial of N is the product of the number itself and all its preceding numbers we can run loop i, from 1 to N. In every iteration current i, is multiplied with the product so far.
Below is the implementation for the above approach:
C++
// C++ program for factorial of a number
#include <iostream>
using namespace std;
// function to find factorial of given number
long long int factorial(int n)
{
long long int res = 1, i;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
// Driver code
int main()
{
int num = 5;
cout << "Factorial of " << num << " is "
<< factorial(num) << endl;
return 0;
}
C
#include <stdio.h>
// function to find factorial of given number
long long int factorial(int n)
{
long long int res = 1, i;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
int main()
{
int num = 5;
printf(
"Factorial of %d is %d", num, factorial(num));
return 0;
}
Java
// Java program to find factorial of given number
class Test {
// Method to find factorial of the given number
static long factorial(int n)
{
long res = 1, i;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println(
"Factorial of " + num
+ " is " + factorial(5));
}
}
Python
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
def factorial(n):
res = 1
for i in range(1, n+1):
res *= i
return res
# Driver Code
num = 5;
print("Factorial of", num, "is",factorial(num))
C#
// C# program to find
// factorial of given number
using System;
class Test {
// Method to find factorial
// of given number
static long factorial(int n)
{
long res = 1, i;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
// Driver method
public static void Main()
{
int num = 5;
Console.WriteLine(
"Factorial of " + num
+ " is " + factorial(5));
}
}
JavaScript
// JavaScript program to find factorial of given number
// Method to find factorial of the given number
function factorial(n)
{
var res = 1, i;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
// Driver method
var num = 5;
console.log("Factorial of " + num + " is "
+ factorial(5));
PHP
<?php
// function to find factorial of given number
function factorial($n)
{
$res = 1;
for ($i = 1; $i <= $n; $i++) {
$res *= $i;
}
return $res;
}
// Driver code
$num = 5;
echo "Factorial of $num is " . factorial($num) . "\n";
?>
OutputFactorial of 5 is 120
Time Complexity: O(n), since we are running a loop from 1 to n.
Auxiliary Space: O(1)
The above solution cause overflow for large numbers. Please refer factorial of large number for a solution that works for large numbers.
Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
Please Login to comment...