Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3. (a) (3 points) Write a program named powers.py that gets user input for two integers a and b, then computes and displays the value
3. (a) (3 points) Write a program named powers.py that gets user input for two integers a and b, then computes and displays the value of ab. You may assume that both a and b are non-negative. Sounds pretty easy, right? The catch here is that you cannot use Python's ** operator, or any other built-in way of finding powers. Instead, compute the result by repeatedly multiplying aaa for b times. Your code should work for any non-negative values of a and b, including zero. (Remember that anything to the 0th power is 1 .) Hint: Create a variable to store the result, and initialize it to 1. Then write a loop that repeats b times. Each iteration should multiply the result by a. (b) (3 points) Write a program named factorials.py that gets user input for an integer n, then computes and displays the value of n ! You may assume that n is non-negative. n ! (pronounced " n factorial") is defined as n(n1)(n2)1. For example, 5!=54321=120. Your program should work for any reasonably small non-negative value of n, including zero (note that 0 ! has a value of 1 ). Hint: This is very similar to the previous part. The only difference is that instead of multiplying by the same number every iteration, you're multiplying by something that changes. Use a variable to hold that changing value
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started