# Python Exercise: Calculate the factorial of a number

## Python Functions: Exercise-5 with Solution

Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.

**Sample Solution-1:**

**Python Code:**

```
# Define a function named 'factorial' that calculates the factorial of a number 'n'
def factorial(n):
# Check if the number 'n' is 0
if n == 0:
# If 'n' is 0, return 1 (factorial of 0 is 1)
return 1
else:
# If 'n' is not 0, recursively call the 'factorial' function with (n-1) and multiply it with 'n'
return n * factorial(n - 1)
# Ask the user to input a number to compute its factorial and store it in variable 'n'
n = int(input("Input a number to compute the factorial: "))
# Print the factorial of the number entered by the user by calling the 'factorial' function
print(factorial(n))
```

Sample Output:

Input a number to compute the factiorial : 4 24

**Explanation:**

In the exercise above the code defines a recursive function named "factorial()" that calculates the factorial of a given number 'n'. It prompts the user to input a number and then computes and prints the factorial of that input number using the "factorial()" function.

**Pictorial presentation:**

**Flowchart: **

**Sample Solution-2:**

- Use recursion.
- If n is less than or equal to 1, return 1.
- Otherwise, return the product of n and the factorial of n - 1.
- Display an error message if n is a negative or a floating point number.

**Python Code:**

```
# Define a function named 'factorial' that calculates the factorial of a non-negative integer 'n'
def factorial(n):
# Check if the number 'n' is negative or a floating-point number
if not ((n >= 0) and (n % 1 == 0)):
# If 'n' is negative or a floating-point number, return an error message
return("Number can't be negative or floating point!")
# Calculate the factorial using a ternary operator: 1 if n is 0, else n * factorial(n - 1)
return 1 if n == 0 else n * factorial(n - 1)
# Print the factorial of 5 by calling the 'factorial' function with argument 5
print("\nFactorial of 5: ", factorial(5))
# Print the factorial of -12 by calling the 'factorial' function with argument -12
print("\nFactorial of -12: ", factorial(-12))
# Print the factorial of 1.22 by calling the 'factorial' function with argument 1.22
print("\nFactorial of 1.22: ", factorial(1.22))
# Print the factorial of 100 by calling the 'factorial' function with argument 100
print("\nFactorial of 100: ", factorial(100))
```

Sample Output:

Factorial of 5: 120 Factorial of -12: Number can't be negative or floating point! Factorial of 1.22: Number can't be negative or floating point! Factorial of 100: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

**Explanation:**

In the exercise above the code defines a function named "factorial()" that checks if the input number is a non-negative integer. It calculates the factorial if the number meets the criteria and returns an error message otherwise. Then, it demonstrates the use of the "factorial()" function by calculating the factorials of different numbers and printing the results.

**Flowchart: **

**Python Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:** Write a Python program to reverse a string.

**Next:** Write a Python function to check whether a number falls in a given range.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics