0% found this document useful (0 votes)
404 views

Assignment 9

This Python program uses recursion to calculate factorials. It prompts the user to input a non-negative integer, calls a recursive function to calculate the factorial, and prints the result. The program then asks if the user wants to calculate another factorial, looping until the user enters 'N'.

Uploaded by

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

Assignment 9

This Python program uses recursion to calculate factorials. It prompts the user to input a non-negative integer, calls a recursive function to calculate the factorial, and prints the result. The program then asks if the user wants to calculate another factorial, looping until the user enters 'N'.

Uploaded by

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

Pseudocode:

START
Set loop = 1
While loop = 1:
define recursion(x)
If x == 1:
return 1
else:
return (x * recursion(x 1))
print message
num = int(input())
if num >= 1:
Print recursion(num)
Prompt user for another num to factorialize
if yes :
loop = 1
else:
loop = 0
else:
Prompt user for a non-negative integer
endwhile
END

START

loop = 1

YES
While loop == 1

Define recursive(x)

If x == 1

NO

recursive = x * (recursive(x 1))

YES
NO
recursive = 1

Print Let s Factorialize!

Print Enter a nonnegative integer:

num = int(input())

If num >= 1

NO

YES

Call recursion(num)

Print The factorial of


num is recursion(num)

END

Print Would you like to


find the factorial of
another number?
answer = input

NO

If answer = Y , y , Yes ,
YES , yes

Python Code:
# Let's Factorialize!
loop = 1
while loop == 1:
# Recursive function for factorialization
def recursion(x):
if x == 1:
return 1
else:
return(x * recursion(x - 1))
print("Let's Factorialize")
num = int(input("Enter a non-negative integer: "))
if num >= 1:
print("The factorial of", num, "is", recursion(num))
print()
answer = input("Would you like to find the factorial of another non-negative integer? Y/N: ")
print()
# Error checking user input for continuation of the factorial program
if answer in ("Y", "y", "YES", "Yes", "yes"):
loop = 1
else:
loop = 0
# Error checking user input for initial input variable >0
else:
print("You did not enter a non-negative integer. Please try again.")

YES

Print You did not enter


a non-negative integer

You might also like