This Python program calculates the factorial of a given number. Factorial is a mathematical operation that calculates the product of all positive integers up to a given number. For example, the factorial of 5 (denoted as 5!) is equal to 5 x 4 x 3 x 2 x 1 = 120.

top of page

bottom of page

This Python program calculates the factorial of a given number. Factorial is a mathematical operation that calculates the product of all positive integers up to a given number. For example, the factorial of 5 (denoted as 5!) is equal to 5 x 4 x 3 x 2 x 1 = 120.

This program defines a function called factorial() which takes a number n as input and returns its factorial. It checks if the input number is negative and raises a ValueError in such cases since factorial is not defined for negative numbers. If the input number is 0 or 1, the function returns 1 as the factorial. For any other positive number, it uses a for loop to calculate the factorial iteratively. The result is then printed to the console.

To use this program, simply run it and enter a number when prompted. The program will calculate the factorial and display the result.