Power of a number

The power of a number represents the number of times to use that number in a multiplication. Usually, power is represented with a base number and an exponent.

Logic

Let’s declare one temporary variable power with value 1, we have used the data-type long so that it can hold a big long value.

To calculate the power of a number we need a base and an exponent value, we are taking these two values from the user, after taking input (base, exponent) from the user we start one loop from 1 to exponent.

Inside the loop, for every iteration, we multiply the base by power (base * power) and store the result again in variable power until the loop is completed.

Once the loop is over, simply print the variable power containing the resultant power value of a given number.

Program

import math

# Take input from user
base = int(input("Enter base : "))
exponent = int(input("Enter exponent : "))

power = 1

for i in range(1, exponent + 1):
    power = power * base

print("\nResult", base, "^", exponent, ":", power)
print("Using math class :", math.pow(base, exponent))

Output

Enter base and exponent :
Result 2 ^ 5 : 32
Using Math class : 32.0