Check if a number is a power of another number | C ++ program to check if a number is power of another number ; Returns 1 if y is a power of x ; The only power of 1 is 1 itself ; Repeatedly comput power of x ; Check if power of x becomes y ; Driver program to test above function ; check the result for true / false and print
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int x , long int y ) { if ( x == 1 ) return ( y == 1 ) ; long int pow = 1 ; while ( pow < y ) pow *= x ; return ( pow == y ) ; } int main ( ) { check the result for true / false and print cout << isPower ( 10 , 1 ) << endl ; cout << isPower ( 1 , 20 ) << endl ; cout << isPower ( 2 , 128 ) << endl ; cout << isPower ( 2 , 30 ) << endl ; return 0 ; }