codescracker


python

Python Program to Find Factorial of Number



« Previous Program Next Program »


Find Factorial of Number in Python

To find factorial of any number in python, you have to ask from user to enter the number to find and print the factorial of that number on the output screen.

Python Programming Code to Find Factorial of Number

Following python program ask from user to enter a number to find the factorial of that number:

# Python Program - Find Factorial of a Number

print("Enter 'x' for exit.");
num = input("Enter a number to find its factorial: ");
if num == 'x':
    exit();
else:
    number = int(num);
    if number == 0:
        print("\nFactorial of 0 is 1");
    elif number < 0:
        print("\nFactorial of negative numbers doesn't exist..!!");
    else:
        fact = 1;
        for i in range(1, number+1):
            fact = fact*i;
        print("\nFactorial of", number, "is", fact);

Here is the sample run of the above Python program to demonstrate how to find factorial of a number:

find factorial of number python

Now let's enter any number say 5 to find its factorial as shown in the below sample output:

factorial of number python

As you all know that factorial of a number say n can be calculated in this way:

n x (n-1) x (n-2)...3 x 2 x 1

Therefore, factorial of 5 can be calculated as:

5 x 4 x 3 x 2 x 1

will be sorted as

20 x 3 x 2 x 1

will again be sorted as

60 x 2 x 1

will be 120 as factorial value of 5 that you have already seen in the sample output given above.

Below is the same program on python shell:

python find factorial of number

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test