xxxxxxxxxx
int factorial(int n) {
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
xxxxxxxxxx
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
number = 5
result = factorial(number)
print("Factorial of", number, "is", result)
xxxxxxxxxx
// Calculate factorial
fn factorial(n: u64) -> u64 {
(1..=n).product()
}
fn main() {
println!("Factorial = {} ", factorial(5) );
}
xxxxxxxxxx
public class FactorialCalculator {
public static void main(String[] args) {
int n = 5;
long factorial = calculateFactorial(n);
System.out.println("The factorial of " + n + " is " + factorial);
}
public static long calculateFactorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
}
if (n == 0 || n == 1) {
return 1;
}
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}
xxxxxxxxxx
# Calculate factorial
import math
data = [1, 2, 3, 4, 5, 6]
print(list(map(math.factorial, data)))