Given a non-negative integers n, compute the factorial of the given number.
Note: Factorial of n is defined as n * (n -1) * (n - 2) * ... * 1. For n = 0, the factorial is defined as 1.
Examples:
Input: n = 5
Output: 120
Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120Input: n = 4
Output: 24
Explanation: 4! = 4 * 3 * 2 * 1 = 24
Table of Content
Iterative Solution - O(n) Time and O(1) Space
Factorial is computed by multiplying all integers from 1 to n using a loop. We initialize a variable ans as 1 and update it in each iteration by multiplying with the current number. This approach avoids recursion and uses constant extra space.
Step-by-step execution:
For n = 4
- Initialize : ans = 1
- i = 1, ans = 1 * 1 = 1
- i = 2, ans = 1 * 2 = 2
- i = 3, ans = 2 * 3 = 6
- i = 4, ans = 6 * 4 = 24
Final factorial = 24
#include <iostream>
using namespace std;
int factorial(int n) {
// Calculating factorial of number
int ans = 1;
for (int i = 2; i <= n; i++) {
ans = ans * i;
}
return ans;
}
int main()
{
int num = 5;
cout << factorial(num) << endl;
return 0;
}
#include <stdio.h>
int factorial(int n)
{
// Calculating factorial of number
int ans = 1, i;
for (i = 2; i <= n; i++)
ans *= i;
return ans;
}
int main()
{
int num = 5;
printf("%d\n", factorial(num));
return 0;
}
class GfG {
public static int factorial(int n) {
// Calculating factorial of number
int ans = 1;
for (int i = 2; i <= n; i++) {
ans = ans * i;
}
return ans;
}
public static void main(String[] args)
{
int num = 5;
System.out.println( factorial(5));
}
}
def factorial(n):
ans = 1
i = 2
#Calculating factorial of number
while (i <= n):
ans *= i
i += 1
return ans
if __name__ == "__main__":
num = 5
print(factorial(num))
using System;
class GFG {
public static int factorial(int n) {
// Calculating factorial of number
int ans = 1;
for (int i = 2; i <= n; i++) {
ans = ans * i;
}
return ans;
}
public static void Main()
{
int n = 5;
Console.WriteLine(factorial(n));
}
}
function factorial(n) {
// Calculating factorial of number
let ans = 1;
for (let i = 2; i <= n; i++) {
ans = ans * i;
}
return ans;
}
// Driver method
let num = 5;
console.log(factorial(5));
Output
120
Recursive Solution - O(n) Time and O(n) Space
Factorial is defined recursively as n! = n × (n - 1)!. We define a base case where if n equals 0 or 1, the function returns 1. Otherwise, the function calls itself with n minus 1, breaking the problem into smaller subproblems until reaching the base case.

#include <iostream>
using namespace std;
int factorial(int n)
{
// Calculating factorial of number
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main()
{
int num = 5;
cout << factorial(num) << endl;
return 0;
}
#include <stdio.h>
int factorial(int n)
{
// Calculating factorial of number
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int num = 5;
printf("%d\n", factorial(num));
return 0;
}
class GFG {
static int factorial(int n)
{
// Calculating factorial of number
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
public static void main(String[] args)
{
int num = 5;
System.out.println(factorial(num));
}
}
def factorial(n):
# Calculating factorial of number
if n == 0:
return 1
return n * factorial(n - 1)
if __name__ == "__main__":
num = 5
print(factorial(num))
using System;
class GFG {
static int factorial(int n)
{
// Calculating factorial of number
if (n == 0)
return 1;
return n * factorial(n - 1);
}
public static void Main()
{
int n = 5;
Console.WriteLine(factorial(n));
}
}
function factorial(n)
{
// Calculating factorial of number
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver Code
let num = 5;
console.log(factorial(num));
Output
120