Problème
The absolute value of a number is its distance from zero. Compute it with an if statement instead of a built-in abs function.
Implement absolute_value(n).
Input
n: Any integer.
Output
Return n if it is zero or positive, otherwise -n.
Contraintes
-1,000,000,000 ≤ n ≤ 1,000,000,000
Exemples
Exemple 1
Entrée :
n = -42
Sortie attendue :
expected = 42
Entrée :
long long n = -42;
Sortie attendue :
long long expected = 42;
Entrée :
long n = -42;
Sortie attendue :
long expected = 42;
Explication
-42 is negative, so the answer is 42.
Exemple 2
Entrée :
n = 15
Sortie attendue :
expected = 15
Entrée :
long long n = 15;
Sortie attendue :
long long expected = 15;
Entrée :
long n = 15;
Sortie attendue :
long expected = 15;
Explication
15 is already positive, so it stays 15.
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.