Check if a number is a power of another number | C # program to check if a number is power of another number ; Returns true if y is a power of x ; The only power of 1 is 1 itself ; Repeatedly compute power of x ; Check if power of x becomes y ; Driver Code ; check the result for true / false and print .
using System ; class GFG { public static bool isPower ( int x , int y ) { if ( x == 1 ) return ( y == 1 ) ; int pow = 1 ; while ( pow < y ) pow = pow * x ; return ( pow == y ) ; } public static void Main ( ) { Console . WriteLine ( isPower ( 10 , 1 ) ? 1 : 0 ) ; Console . WriteLine ( isPower ( 1 , 20 ) ? 1 : 0 ) ; Console . WriteLine ( isPower ( 2 , 128 ) ? 1 : 0 ) ; Console . WriteLine ( isPower ( 2 , 30 ) ? 1 : 0 ) ; } }