w3resource logo


Python Exercises

Python Challenges: Check if an integer is the power of another integer

Python Challenges - 1: Exercise-5 with Solution

Write a Python program to check if an integer is the power of another integer.

Input : 16, 2
Output : True

Example: For x = 16 and y = 2 the answer is "true", and for x = 12 and y = 2 "false"

Explanation :

Python: Power of another integer

Sample Solution :-

Python Code :

def is_Power(x, y):
   while (x%y == 0):
       x = x / y
   return x == 1
print(is_Power(16, 2))
print(is_Power(12, 2))
print(is_Power(81, 3))

Output :

True                                                                    
False                                                                   
True 

Flowchart:

Python Flowchart: Check if an integer is the power of another integer

Python Code Editor:

def is_Power(x, y):
   while (x%y == 0):
       x = x / y
   return x == 1
print(is_Power(16, 2))
print(is_Power(12, 2))
print(is_Power(81, 3))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.