187k views
1 vote
Write a program that asks the user to input a positive integer and then calculates and displays the factorial of the number. The program should call a function named getN

1 Answer

4 votes

Answer:

The program is written in python and it doesn't make use of any comment;

(See explanation section for line by line explanation)

def getN(num):

fact = 1

for i in range(1, 1 + num):

fact = fact * i

print("Factorial: ",fact)

num = int(input("Number: "))

if num < 0:

print("Invalid")

else:

getN(num)

Step-by-step explanation:

The function getNum is defined here

def getN(num):

Initialize the result of the factorial to 1

fact = 1

Get an iteration from 1 to the user input number

for i in range(1, 1 + num):

Multiply each number that makes the iteration

fact = fact * i

Print result

print("Factorial: ",fact)

Ths line prompts user to input number

num = int(input("Number: "))

This line checks if user input is less than 0; If yes, the program prints "Invalid"

if num < 0:

print("Invalid")

If otherwise, the program calls the getN function

else:

getN(num)

User Jon Snow
by
3.7k points