← Unit 2 · all exercises
Saved in this browser only

Absolute value, by hand

Return the number without its sign, without calling Math.abs. If it is negative, give back its opposite; otherwise give it back unchanged.

int absolute(int n)

Solution.java
Loading editor…
Results

Examples

  • absolute(5) → 5
  • absolute(-5) → 5

There are also 4 hidden tests you can't see. Solve the problem, not the examples.