public static void Factorial(int n)
{
var number = new int[999999];
number[0] = 1;
int numLength = 1;
for (int times = 2; times <= n; times++)
numLength = Multiply(times, number,numLength);
for (int i = numLength - 1; i >= 0; i--)
Console.Write(number[i]);
}
static int Multiply(int times, int[] number,int numLength)
{
int carry = 0;
for (int i = 0; i < numLength; i++)
{
int product = number[i] * times + carry;
number[i] = product % 10;
carry = product / 10;
}
while (carry != 0)
{
number[numLength] = carry % 10;
carry /= 10;
numLength++;
}
return numLength;
}
# Factorial of a number using recursion
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
<?php
$num = 3;
$factorial = 1;
if($num != 0) {
for($i = $num; $i>=1; $i--) {
$factorial = $factorial * $i;
}
}
else {
$factorial = 1;
}
echo "Factorial of $num is $factorial";
?>