How to find out whether a number b is can be expressed as a power of another number c and to find the corresponding exponent? Without using math.h or recursion.
The numbers are of the type int!
This is the code I have written:
#include <stdbool.h>
bool is_apowb(int a, int b, int *e) {
int x =0;
if (a>b)
{
while (a%b==0 && b>0)
{
a=a/b;
x++;
}
*e=x;
return true;
}
else if (a==b)
{
*e=1;
return true;
}
else if (b<0)
{
while (a%b==0)
{
a=a/b;
x++;
}
*e=x;
if (x%2==0)
{
return true;
}
else return false;
}
return false;
}
The code is failing the following tests:
assert(is_apowb(9,-3,&e));
assert(e == 2);
assert(!is_apowb(8,-2,&e));
assert(e == 2);
Thanks in Advance!