Python Program to Find the Factorial of a Number Without Recursion
Question
Python Program to Find the Factorial of a Number Without Recursion
Solution
Sure, here is a simple Python program to find the factorial of a number without using recursion:
# Take input from the user
num = int(input("Enter a number: "))
# Initialize factorial to 1
factorial = 1
# Check if the number is negative, zero or positive
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
This program works by initializing a variable factorial to 1, then repeatedly multiplying it by each integer from 1 to the input number. This is done in a loop that runs from 1 to the input number (inclusive). The result is the factorial of the input number.
Similar Questions
Write a python function to find the factorial of given number
write a recursive function to find factorial of a number.
Factorial of a non-negative integerYou are given a Python code snippet that defines a function to calculate the factorial of a non-negative integer using recursion. However, some parts of the code are missing. Fill in the missing code to complete the program.Sample Test CasesTest Case 1:Expected Output:Enter·a·non-negative·integer:·3Factorial·of·3·is:·6Submit1234567891011121314151617#·Fill·in·the·missing·code·below·to·define·a·recursive·function·to·calculate·the·factorial·of·a·number¬¬def·factorial(n):¬¬····#·Base·case:·If·n·is·0·or·1,·return·1¬····if·n·==·0·or·n·==·1:¬········return·1¬···¬····else:¬········#·Fill·in·the·missing·code·to·calculate·the·factorial·recursively¬········return·n·*·····#·Fill·in·the·missing·code¬¬#·Test·the·function¬number·=·int(input("Enter·a·non-negative·integer:·"))¬¬print("Factorial·of",·number,·"is:",······)¬¶
Write a program to calculate the factorial of N.
Write a Shell script to find Factorial of a given number.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.