Q:

Python program to find the power of a number using loop

0

Here, we are going to calculate the value of Nth power of a number without using power function.

The idea is using loop. We will be multiplying a number (initially with value 1) by the number input by user (of which we have to find the value of Nth power) for N times. For multiplying it by N times, we need to run our loop N times. Since we know the number of times loop will execute, so we are using for loop.

Example:

    Input:
    base: 5, power: 4

    Output:
    625

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python code to find power of a number using loop

num = int(input("Enter the number of which you have to find power: "))
pw = int(input("Enter the power: "))

kj = 1
for n in range(pw):
    kj = kj*num

print(kj)

Output

Enter the number of which you have to find power: 5
Enter the power: 4
625

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now