Python program to find the power ‘n’ of the given number ‘a’

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to convert GCD of the given 2 numbers.

C program to compute sin(x) using Taylor’s series

Code for python program to find the power ‘n’ of the given number ‘a’

def power(a, n):
    result = 1
    for i in range(n):
        result *= a
    return result

# test the function
print(power(2, 3))
print(power(4, 2))

Output

8

16

Explanation

The function power(a, n) takes in two parameters, a and n, and returns a raised to the power of n. It does this by using a for loop that iterates n times, and in each iteration, it multiplies result by a.

The initial value of result is set to 1 so that when the loop starts and the first time a is multiplied, it is multiplied by 1.

Conclusion

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment