Home » Python program to calculate a^b

Python program to calculate a^b

Power a to b using iteration

def power(a,b):
    res=1
    if b==0:
        return 1
    else:
        for i in range(b):
            res*=a
    return res

#__main__
print("Enter only positive numbers")
num=int(input("Enter base number:"))
p=int(input("raised to the power of:"))
result=power(num,p)
print(num,"raised to the power of",p,"is",result)

Contents

Output:

Enter only positive numbers
Enter base number:8
raised to the power of:2
8 raised to the power of 2 is 64