Q:

Check whether a number is a power of another number or not in Python

belongs to collection: Python basic programs

0

To solve this problem simply, we will use the log() function from the math module. The math module provides us various mathematical operations and here we will use the log() function from this module. In Python working of log() function, is the same as log work in mathematics. Here, the user will provide us two positive values a and b and we have to check whether a number is a power of another number or not in Python. The idea is simple to find the log of a base b and takes the integer part of it and assigns it to a variable s. After this just check if s to the power of b is equal to a then a is the power of another number b. Before going to solve this, we will see the algorithm to solve this problem and try to understand it.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# importing the module
import math

# input the numbers
a,b=map(int,input('Enter two values: ').split())

s=math.log(a,b)

p=round(s)

if (b**p)==a:
    print('{} is the power of another number {}.'.format(a,b))
else:
    print('{} is not the power of another number {}.'.format(a,b))

Output

RUN 1:
Enter two values: 1228 2
1228 is the power of another number 2.
	
RUN 2:
Enter two values: 15625 50
15625 is not the power of another number 50.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Check whether the binary representation of a given... >>
<< Find the number of integers from 1 to n which cont...