Ask Question
27 February, 04:47

Given non-negative integers x and n, x to the nth power can be defined as:

x to the 0th power is 1

x to the nth power can be obtained by multiplying x to the (n-1) th power with x

1. Write a function named power that accepts two parameters containing integer values (x and n, in that order) and recursively calculates and returns the value of x to the nth power.

+2
Answers (1)
  1. 27 February, 08:11
    0
    def power (x, n):

    if n = = 0:

    return 1

    else:

    return x * power (x, n-1)

    print (power (2, 3))

    Explanation:

    Create a function called power that takes two parameters, x and n

    If n is equal to 0, return 1

    Otherwise, multiply x with the function itself, but as a parameter pass x and n-1 for each iteration.

    For example,

    If x = 2 and n = 3, Is 3 = = 0? No, then power (2, 3) = 2 * power (2, 2) (1)

    x = 2 and n = 2, Is 2 = = 0? No, then power (2, 2) = 2 * power (2, 1) (2)

    x = 2 and n = 1, Is 1 = = 0? No, then power (2, 1) = 2 * power (2, 0) (3)

    x = 2 and n = 0, Is 0 = = 0? Yes, power (2, 0) = 1 (4)

    Iteration 4 will give us 1. If you substitute the power (2, 0) in iteration 3 with 1, it becomes 2. If you substitute the power (2, 1) with 2 in iteration 2, it becomes 4. If you substitute the power (2, 2) with 4 in iteration 1, it becomes 8.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Given non-negative integers x and n, x to the nth power can be defined as: x to the 0th power is 1 x to the nth power can be obtained by ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers