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.
Recommended Python Tutorials
Recommended:-Python Program To Print Number Pattern
Recommended:-Python Program to Print Star Pattern
Recommended:-Python Program Calculate Sum of n Numbers
Recommended:-Python Program to Reverse Numbers
Recommended:-Python Program to Find Largest of n Numbers
Recommended:-Python Program to Find Power of Number
Recommended:-Python Program to Print Prime Number From 1 to N
Recommended:-Python Program to Find Square Root of Number
Recommended:-Python Program to Find Cube of Number
Recommended:-Python Palindrome Program
Recommended:-Python Program to Count Set Bits in a Number
Recommended:-Python Program for BMI Calculation
Recommended:-Python Program to Find Out Absolute Value
Recommended:-Python Random Number Generator Code
Recommended:-Python program for Zip, Zap and Zoom game
Recommended:-Python Program to Swap Two Numbers
Recommended:-Python Program to Find n-th term of Fibonacci Series
Recommended:-Python Program For Calculator
Recommended:-Python Program to Find Strong Number
Recommended:-Python Program to Convert cm to Feet and Inches
Recommended:-How to Reverse a List in Python
Recommended:-Python Program to Convert Celsius to Fahrenheit
Recommended:-Python Program to Find Standard Deviation
Recommended:-Python Program to Check Perfect Number
Recommended:-Python Program to Display Calendar
Recommended:-Python Program to Print Alphabets from A to Z