Question

4- Write a program by using function that asks the user for entering a positive integer...

4- Write a program by using function that asks the user for entering a positive integer
number (a) and then calculate the factorial of the number. Draw the Trace Table
(8 Marks)( k/2, T/2, C/2, A/2)
Enter a number: 4
Factorial (4) = 4! =4*3*2*1=24

use python pls

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BECAUSE OF THE IDENTATION PROBLEM WHILE SUBMITTING THE CODE SO THAT IM ALSO GIVING YOU THE SCREENSHOT SO THAT YOU CAN REFER IT :

main.py 1 def factorial(n): # factorial function 2 if n == 1 : # if n is 1 return n 4 else: 5 return n * (n - 1) # recursfive

PYTHON CODE :

def factorial(n): # factorial function
if n == 1 : # if n is 1
return n
else:
return n * (n - 1) # recursive function

n = int(input("Enter the number: ")) # read the number
if n == 0 : # if n is zero
print("Factorial of 0 is : 1")
else:
print("Factorial of",n,"is :",factorial(n)) # print the result

INPUT & OUTPUT :

Enter the number: 4 Factorial of 4 is : 12 ... Program finished with exit code 0 Press ENTER to exit console.

* fact. (n-1) ។ food (3) /l 3 tact (2) / IN 2 fact (1) 4*3*2*1 24

Add a comment
Know the answer?
Add Answer to:
4- Write a program by using function that asks the user for entering a positive integer...
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
  • In Python, write a program that asks the user for a positive integer number. If the...

    In Python, write a program that asks the user for a positive integer number. If the user enters anything else, the program would ask the user to re-enter a number or enter -1 to quit. If it is a positive number, the program then prints out a multiplication table that goes up to that number. For example, if the user enters 10, the output would look something like this. https://www.vectorstock.com/royalty-free-vector/multiplication-table-on-white-background-vector-2721686

  • 4. Write a program that asks the user for a positive integer no greater than 15....

    4. Write a program that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character 'X'. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following: XXXXX XXXXX XXXXX XXXXX XXXXX c++

  • C++ Write a program that asks user for a positive integer side length. If they enter...

    C++ Write a program that asks user for a positive integer side length. If they enter an illegal value, they must be prompted to enter a good one until they do. It then displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4, the program should display:

  • 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...

  • USING PYTHON 1. Write a small program that asks for an integer number from the user...

    USING PYTHON 1. Write a small program that asks for an integer number from the user and print all the prime numbers (2,3,5,7,etc) less than the input number. 2. How long it takes for your program to print the prime numbers less than 100. (Use magic functions)

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

  • Using python Write a Python function called sgm that requests a positive integer n and returns...

    Using python Write a Python function called sgm that requests a positive integer n and returns the series geometric mean of the numbers 1,2,3, . . . n. For example sgm(5) = (5 x 4 x 3 x 2 x 1)1/5 = (120) 1/5 = 2.605 . Write the program so that a user is requested for a positive integer and program prints the series geometric mean of the integer. Example execution: Enter number: 5 series geometric mean of 5...

  • This is for Program in C Write a program that asks the user for an integer...

    This is for Program in C Write a program that asks the user for an integer with the prompt "Please enter an integer" using the puts function. Write the definition of a function isSenior, which receives an integer parameter and returns true if the parameter's value is greater or equal to 65, and false otherwise. So if the parameter's value is 7 or 64 or 12 the function returns false. But if the parameter's value is 69 or 83 or...

  • need this by tonight pls using python Write a program that asks the user to enter...

    need this by tonight pls using python Write a program that asks the user to enter a month (1 for January, 2 for February, and so on) and then prints the number of days in the month. For February, print “28 or 29 days”. Enter a month: 5 30 days Do not use a separate if/else branch for each month. Use Boolean operator

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