Confronting with numbers is the first thing each one of us has learned in school. However, as we step up high more and more difficult questions based on the same fundamentals seem to appear in our way. It’s the same with programming, using the same fundamentals to find solutions for multiple complex problems. Now the cool thing is, you can be creative in your way eventually coming up with a unique solution. And if you’re able to do this, then you’ve clearly understood how to use programming for others and your good. But keeping that aside, we’ve talked about numbers before. So what if we find exponential numbers, seems too easy right!. So let’s quickly find out how to Print Exponential Of A Number in Python.
With the help of this program, you will be able to find the root square of integers. Practicing these types of questions also helps to get an upper edge in Competitive Programming.
What’s The Approach?
- Let us consider
xto be the number &
yto be the exponent.
- Now recursively firstly we’ll check if
y % 2 == 0, if yes then we’ll return
power(x, y / 2) * power(x, y / 2)
- If not, then we’ll simply multiply x with the function recursively twice while dividing the value of exponent by 2. i.e,
x * power(x, y / 2) * power(x, y / 2)
Also Read: Print Cube Root of A Number in Python
Python Program To Print Exponential of A Number
x = 2, y = 3
# Python program to print Exponential of A Number # Function to calculate x # raised to the power y def power(x, y): if (y == 0): return 1 elif (int(y % 2) == 0): return (power(x, int(y / 2)) * power(x, int(y / 2))) else: return (x * power(x, int(y / 2)) * power(x, int(y / 2))) # Driver Code x = 2; y = 3 print(power(x, y))