Question

Write a python program to compute the factorial of a given non-negative integer n. If the...

  1. Write a python program to compute the factorial of a given non-negative integer n. If the user inputs any integer outside the required, print “Error! Try again!”
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have provided the required code below.

All the steps have been explained via the comments, however, please ask in the comments if you need any clarifications. Thank you.

THE CODE:

def fact(n):

    #function to recurisively calculate the factorial of a number

    if (n==1 or n==0):

        #return 1 if factorial of 0 or 1 is needed

        return 1

    else:

        #recursively calls the factorial function

        return(n * fact(n - 1))

#takes input from the user

num = int(input('Enter a non-negative number: '))

#checks whether the input is non-negative

#if yes, prints "Error! Try again. " and queries for a new number until a postive one is provided.

if(num < 0):

    while(num<0):

        print("Error! Try again. ")

        num = int(input('Enter a non-negative number: '))

#prints the final result

print("Factorial of " + str(num) + " is " +  str(fact(num)))

CODE SCREENSHOT:

CODE OUTPUT:

NOTE: Please look at the code screenshot to understand the indentation in a better way.

Add a comment
Know the answer?
Add Answer to:
Write a python program to compute the factorial of a given non-negative integer n. If the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • DONE IN PYTHON The factorial of a non-negative integer n, denoted by n!, is the product...

    DONE IN PYTHON The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5 ! = 5 × 4 × 3 × 2 × 1 = 120. The value of 0! is 1. Write a program, with comments, to do the following: 20 points Ask the user to enter a positive integer n. between 1 and 20 ; You may assume the user will enter...

  • Using python for this programming Factorial Ask the user to enter an integer number. Then print...

    Using python for this programming Factorial Ask the user to enter an integer number. Then print the factorial of that number Write a program to print the below ********                      (note: 8 stars) ******* ****** ***** **** *** ** * Ask the user to enter the speed of their car. Write an if-else statement that displays “Speed is normal” if the speed variable is within the range 24 to 56. If speed variable is outside this range it should display “speed...

  • Java program 5. Write a short Java program to compute factorial of a given integer from...

    Java program 5. Write a short Java program to compute factorial of a given integer from input using recursive function.

  • If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1)...

    If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1) * (n-2) * ( n-3)… * By convention, 0! = 1. You must write a program that allows a user to enter an integer between 1 and 7. Your program must then compute the factorial of the number entered by the user. Your solution MUST actually perform a computation (i.e., you may not simply print “5040” to the screen as a literal value if...

  • Write a C++ function to find factorial of an integer number N >0. Using the factorial...

    Write a C++ function to find factorial of an integer number N >0. Using the factorial function print the factorial of two inputs as shown below: Sample input: 1 4 Expected output: 1 24

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • In python, Write a program to output grades A, B, C, D, F depending on the...

    In python, Write a program to output grades A, B, C, D, F depending on the scores to be lying between 90 and 100, 80 and 89, 70 and 79, 60 and 69 (inclusive of the end points). If the user inputs numbers less than zero or greater than 100, print “Error! Try again!” Thank you

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT