basics · beginner · ~15 min
Loop accumulation patterns.
Raise an integer to a non-negative power using a loop.
Implement long ipow(int base, int exp) that returns base raised to the power exp, computed by iterative multiplication. By definition any base to the power 0 is 1.
An int base and a non-negative int exp.
Returns base^exp as a long.
ipow(2, 10) -> 1024
ipow(3, 5) -> 243
ipow(7, 0) -> 1
ipow(10, 6) -> 1000000
exp == 0 returns 1 for any base.An int base and a non-negative int exp.
base raised to exp, as a long.
exp is non-negative. base^0 == 1.
long ipow(int base, int exp) { /* TODO */ return 0; }
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.