Write a program to use the loop to find the factorial of a given number.

preview_player
Показать описание
Here's a Python program to find the factorial of a given number without using a function definition:

python
Copy code
n = int(input("Enter a number: "))
factorial = 1
for i in range(1,n+1):
factorial *= i
print("Factorial of",n,"is",factorial)
In this program, we first take input from the user using the input function and convert it to an integer using the int function. We then initialize the factorial variable to 1.

Next, we use a for loop to iterate over the range of numbers from 1 to n+1. For each value of i in this range, we multiply factorial by i.

After the loop completes, we print out the result using the print function. This program works by directly computing the factorial without using a recursive function.
Рекомендации по теме