w3resource
Python Exercises

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:-

Python Code:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))

Sample Output:

Input a number to compute the factiorial : 4                                                                  
24

Pictorial presentation:

Python exercises: Calculate the factorial of a number.

Flowchart:

Flowchart: Python exercises: Calculate the factorial of a number.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

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 is in a given range.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming


bedava porno