CHECK IF A UMBER IS A POWER OF ANOTHER GIVEN NUMBER IN CPP:
effective way in case of two's power
#include<bits/stdc++.h>
using namespace std;
bool power(int n,int m)
{
int c=0,i;
if (n == 0)
return 0;
while (n != 1)
{
if (n%m != 0)
return 0;
n = n/m;
}
return 1;
}
int main()
{ int t,i;
cin>>t;
for(i=0;i<t;i++)
{
int m,n; //m is which power you want to check and n is the limit
cin>>m>>n;
power(n,m)?cout<<"YES"<<endl:cout<<"NO"<<endl;
}
return 0;
}
OUTPUT:
0 Comments