easy +8 pts

Round to nearest ten

Implement a function that rounds any integer to the nearest multiple of ten.

Write a function `round_to_nearest_ten(n)` that takes an integer `n` and returns the nearest multiple of 10. If there is a tie (i.e., the number ends with 5), round up to the next multiple of ten. The result must be an integer. Examples: - `round_to_nearest_ten(12)` returns `10` - `round_to_nearest_ten(17)` returns `20` - `round_to_nearest_ten(15)` returns `20` (tie rounds up) - `round_to_nearest_ten(0)` returns `0` - `round_to_nearest_ten(-15)` returns `-10` (tie rounds up toward positive infinity, so -10 is the nearest multiple of ten greater than -15) **Function signature:** `def round_to_nearest_ten(n: int) -> int:`

Constraints

- `n` is an integer (may be negative, zero, or positive). - The function must return an integer. - The implementation should work in O(1) time and O(1) space.

Example

>>> round_to_nearest_ten(12)
10
>>> round_to_nearest_ten(17)
20
>>> round_to_nearest_ten(15)
20
>>> round_to_nearest_ten(-15)
-10
>>> round_to_nearest_ten(0)
0
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Consider using integer division and the remainder (modulo) to find the distance to the lower multiple of ten.
For a tie (remainder exactly 5), you need to round up, which means moving toward positive infinity.
If the remainder is less than 5, round down; if it is 5 or more, round up.
Be careful with negative numbers: for example, -15 should round to -10 (not -20).
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.