0

Hi Stack overflow community recently came across this question.. where the interviewer asked me to find the power of the given number without any loop or recursive call no inbuilt.

Example :2^3 = 8 , 3 ^ 3 = 27

please let me know if there is a ans :)

My approach was

int val = 2;
int pow = 3;
while (pow != 0)
{
result *= val;
--pow;
}
  • If the base is 2 you need only to bitwise left shift the amount of power. bitwise left shift is like multiplying by 2. – user1984 Nov 18 '21 at 11:17
  • Can always just use `Math.pow(double a, double b)` https://www.baeldung.com/java-math-pow – Igor Flakiewicz Nov 18 '21 at 11:19
  • no inbuilt methods too and it can be any number, 2 , 3 , 6 any – Supreeth S Nov 18 '21 at 11:22
  • "without any loop or recursive call no inbuilt" .. that leaves approximately nothing. Unrolled loop maybe, but that's basically a loop that we're not writing as a loop - a cheat. – harold Nov 18 '21 at 11:28
  • is this a java specific question expecting you to solve it using a java feature/library or is it some general technique like bit manipulation? – user1984 Nov 18 '21 at 12:16

0 Answers0