Programs · Program 7-10
Q.Program 7-10: Write a program using user defined function calcPow() that accepts base and exponent as arguments and returns the value Base^exponent where Base and exponent are integers.
Tamil Nadu DgeTextbookSubjective· 3mImportance★★★★★est
30% · 12/40 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Program 7-10 computes Base^exponent by repeated multiplication inside a user-defined function that returns the value — calcPow(3, 4) returns 81.
The idea. Exponentiation with an integer exponent is just multiplication repeated exponent times, so the multiplicative-accumulator pattern (start at 1) does the job. Unlike the earlier display-only programs, calcPow() returns its result, so the caller can print it, store it, or use it in a bigger expression.
# Program 7-10: base raised to exponent using a user defined function
def calcPow(base, exponent):
result = 1
for i in range(exponent): # runs exponent times
result = result * base
return result
b = int(input("Enter the base: "))
e = int(input("Enter the exponent: "))
print(b, "raised to", e, "is", calcPow(b, e))
Dry run for base = 3, exponent = 4:
| pass (i) | result |
|---|---|
| start | 1 |
| 0 | 3 |
| 1 | 9 |
| 2 | 27 |
| 3 | 81 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.