Question: How will you implement pow(a,b) without using multiplication or division operators. You can use only add/substract operators.

Solution:

Using recursion, we can do it.

#include

/* assumption: b is greater than or equal to 0*/
int pow(int a, int b)
{
if(b == 0)
return 1;
return multiply(a, pow(a, b-1));
}

/* assumption: y is greater than or equal to 0*/
int multiply(int x, int y)
{
if(y == 0)
return 0;
return (x + multiply(x, y-1));
}

int main()
{
printf("\n %d", pow(5, 3));
getchar();
return 0;
}