Function to Compute Factorial - Python Programming Exercise

In this exercise, you will develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as: 𝑛 ! = 𝑛 × ( 𝑛 − 1 ) × ( 𝑛 − 2 ) × ( 𝑛 − 3 ) × … × 3 × 2 × 1 For example, $$6! = 6 \times 5 \times 4 \times 3 \times 2 \times 1$$ Create a function to calculate the factorial of the number specified as a parameter. This exercise is perfect for practicing function definition, recursion, and arithmetic operations in Python. By implementing this function, you will gain hands-on experience in handling function definitions, recursion, and arithmetic operations in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Compute Factorial

 Objective

Develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as:

n! = n × (n-1) × (n-2) × (n-3) × ... × 3 × 2 × 1

For example, 6! = 6 × 5 × 4 × 3 × 2 × 1

Create a function to calculate the factorial of the number specified as a parameter. For instance, if you call factorial(6), it should display 720.

 Example Python Exercise

 Copy Python Code
# Define the recursive factorial function
def factorial(n):
    # Base case: factorial of 0 or 1 is 1
    if n == 0 or n == 1:
        return 1
    else:
        # Recursive case: n! = n * (n-1)!
        return n * factorial(n - 1)

# Main function to test the factorial function
def main():
    number = 6
    print(factorial(number))  # This should print 720

# Call the main function to execute the program
if __name__ == "__main__":
    main()

 Output

720

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Main Function Parameters and Reversal

    In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise...

  •  Function to Obtain Integer Value

    In this exercise, you will develop a Python program with a function named "get_int" that displays the text received as a parameter, prompts the user for an integer, a...

  •  Function to Manage Task Database

    In this exercise, you will develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability. Th...

  •  Function to Find Maximum Value in an Array

    In this exercise, you will develop a Python program containing a function that accepts a list of floating-point numbers as input and returns the maximum value within ...

  •  Iterative Implementation of the Factorial Function

    In this exercise, you will develop a Python program that calculates the factorial of a given number using an iterative (non-recursive) method. This exercise is...

  •  Function to Print Title

    In this exercise, you will develop a Python program with a function called 'WriteTitle' that displays a text in uppercase, centered on the screen with extra spaces an...