Write a Python program to find the exponentiation of a number.
Program Algorithm:
1. Define a function named power()
2. Read the values of base and exp
3. Use ‘if’ to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result
Program Code:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))
Program Output :
Enter base: 5
Enter exponential value: 2
Result:25
Post A Comment:
0 comments: