Python program to check if an integer is the power of another integer; Through this tutorial, i am going to show you how to check if an integer is the power of another integer in python.
In this tutorial, i will write a python program to check if an integer is the power of another integer using function and while loop.
Program to check if a number is power of another number in python
- Import the math module in the python program.
- Allow user to input values.
- Find the log of a base b and assign its integer part to variable x.
- Also, find the b to the power x and assign it to another variable y.
- Check if y is equal to a then a is a power of another number b and print a is the power of another number b.
# Python program to check if a number is power of another number # import math module import math # input the numbers a,b=map(int,input('Enter two values: ').split()) x=math.log(a,b) y=round(x) if (b**y)==a: print('{} is the power of another number {}.'.format(a,b)) else: print('{} is not the power of another number {}.'.format(a,b))
Output
Enter two values: 1000 10 1000 is the power of another number 10.
Python program to check if a number is power of another number using While loop
In this program, we will use the python while loop with function. After that, allow user to input values. And we have to check whether a number is a power of another number or not in Python by using a function and while loop.
# Python program to check if a number is power of another number # Returns true if y is a power of x def isPower (x, y): # test conditions if (x == 1): return (y == 1) # Repeatedly compute pow = 1 while (pow < y): pow = pow * x # return return (pow == y) a,b=map(int,input('Enter two values: ').split()) # call function and print result if(isPower(a, b)): print('{} is the power of another number {}.'.format(a,b)) else: print('{} is not the power of another number {}.'.format(a,b))
Output
Enter two values: 10 1 10 is the power of another number 1.
Be First to Comment