Absolute Value
Easy · rating 800 · Math
Read an integer n. Print its absolute value.
Editorial
Absolute Value returns a number's magnitude without its sign — negate it when negative, otherwise leave it. The built-in abs is the idiomatic choice.
print(n if n >= 0 else -n)Complexity
Time: O(1). Space: O(1).
Related problems
- Change Due — Easy
- Count Digits — Easy
- Even or Odd — Easy
- Multiply Two — Easy
- Sum of Two — Easy
- Array Sum — Easy