Check if a number is a power of another number
Check if a number is a power of another number
Code:
package sap;
public class Power {
public static void main(String[] args){
int x = 27, y = 729;
int val = (int)Math.log(y)/ (int)Math.log(x);
double res = Math.log(y)/Math.log(x);
if(val == res){
System.out.println(x+" can be expressed as a power of "+y);
}
else{
System.out.println(x +" cannot be expressed as a power of "+y);
}
}
}
Output:
27 can be expressed as a power of 729
Input: x = 10, y = 1 Output: True x^0 = 1 Input: x = 10, y = 1000 Output: True x^3 = 1 Input: x = 10, y = 1001 Output: False
Code:
package sap;
public class Power {
public static void main(String[] args){
int x = 27, y = 729;
int val = (int)Math.log(y)/ (int)Math.log(x);
double res = Math.log(y)/Math.log(x);
if(val == res){
System.out.println(x+" can be expressed as a power of "+y);
}
else{
System.out.println(x +" cannot be expressed as a power of "+y);
}
}
}
Output:
27 can be expressed as a power of 729
Comments
Post a Comment