basics · beginner · ~15 min
Reuse the loop pattern for a product.
Multiply every integer in an inclusive range together.
Implement long product_range(int lo, int hi) that returns the product of all integers from lo to hi inclusive. If lo > hi the range is empty, so return 1 (the empty product). No main — the grader calls it.
The inclusive bounds int lo and int hi.
Returns the product of lo..hi, as a long (1 if lo > hi).
product_range(1, 4) -> 24 (1*2*3*4)
product_range(6, 6) -> 6
product_range(5, 1) -> 1 (empty product)
lo > hi returns 1 (the empty product).The inclusive bounds int lo and int hi.
The product of lo..hi as a long, or 1 if lo > hi.
lo > hi returns 1 (the empty product).
long product_range(int lo, int hi) {
/* TODO */
return 1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.