Write a program to read a number from user and calculate its factorial.
Answers
Answered by
1
Coding:
# Python program to find the factorial of a number provided by the user.
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factors don't exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Answered by
1
class Test
{
static int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(5));
}
}
Similar questions