0% found this document useful (0 votes)
5 views1 page

Python Factorial

This document describes a Python program that calculates the factorial of a given number. It defines a function to compute the factorial using a for loop and prompts the user for input. Instructions for saving and running the program are also provided.

Uploaded by

jeeva nantham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Python Factorial

This document describes a Python program that calculates the factorial of a given number. It defines a function to compute the factorial using a for loop and prompts the user for input. Instructions for saving and running the program are also provided.

Uploaded by

jeeva nantham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

```python

def calculate_factorial(n):
result = 1
for in range(1, n + 1):
result *= i
return result

number = int(input("Enter a number: "))


factorial = calculate_factorial(number)
print(f"The factorial of {number} is: {factorial}")
```

This program does the following:

1. We define a function calculate_factorial that takes a number n as input.


2. Inside the function, we initialize a variable result to 1.
3. We use a for loop to multiply each number from 1 to n (inclusive) with the
current result.
4. After the loop, we return the final result, which is the factorial of the input
number.
5. In the main part of the program, we ask the user to enter a number.
6. We call our calculate_factorial function with the user's input.
7. Finally, we print out the result.

To run this program:

1. Save it as a .py file (e.g., factorial.py)


2. Open a terminal in VSCode
3. Navigate to the directory containing the file
4. Run the command: python factorial.py

This will prompt you to enter a number, and then it will display the factorial of
that number.

You might also like