Beginner · logic
Absolute Value
Absolute value throws the sign away and keeps the size. If the number is already non-negative there is nothing to do; if it is negative you flip it. Two cases, one branch — and the zero case has to land on the right side.
At a glanceIf <code>n < 0</code>, print <code>-n</code>; otherwise print <code>n</code>.
Problem
Input: One integer n (−1000000 ≤ n ≤ 1000000).
Output: The absolute value of n.
Samples:
| Input | Output |
|---|---|
-7 | 7 |
3 | 3 |
Zero is a genuine edge case: its absolute value is 0, not −0. Writing the condition as n < 0 (rather than n <= 0) keeps zero on the harmless side automatically.
Your solution
Custom input for "Run my input"
Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.
Stuck? Hints
Hint 1 — Two cases, not three
Positive and zero need the same treatment (print as is), so they collapse into one branch. Only the negative case is different. Writing three separate branches is a common instinct and always one branch too many.
Hint 2 — Negation is subtraction from zero
-n produces the opposite value without touching n itself. If you would rather be explicit, 0 - n is the same thing. Either way, print the result — do not try to store it back and print later, that is just extra lines.
Hint 3 — Why not just call abs()?
You can — abs() from <cstdlib> works here. But this problem is about the branch, and writing it once by hand makes the three-line library version stop being magic. Try both and confirm they agree.
Solution (try it first!)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n < 0) {
cout << -n << endl;
} else {
cout << n << endl;
}
return 0;
}
Sign handling shows up everywhere: negative remainders in Even and Odd Count, negative divisions in the integer division article, negative distances in Days Between Dates. Getting comfortable with the negative branch early pays off.
Deep dive
Why this works
Absolute value is a two-case definition: non-negative numbers are already their own magnitude, negative numbers need the sign flipped. Only the negative case needs a branch, and -n computes the flip without modifying n.
The cost
O(1) — one comparison, one negation.
Where it goes wrong
- Using
n <= 0instead ofn < 0. It still prints 0 for zero, but the branch now claims zero is negative — and if you later add per-case logic, zero lands on the wrong side. - Assigning
n = -nand then printing inside both branches, duplicating the output line. Print once at the end. - Assuming the answer fits in
int— here it does, because the input range is symmetric. For-2147483648it would not; that is the classicabs(INT_MIN)overflow.
Next step
Sign handling gets subtler when remainders are involved: Even and Odd Count and the integer division article.
Two things to remember
- Two cases suffice: negative flips, everything else does not.
- Prefer
n < 0overn <= 0so zero stays on the harmless side.
🐞 This version fails — open it and try
Only handles the negative case — non-negative input prints nothing
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n < 0) {
cout << -n << endl;
}
// bug: no else branch, so 0 and positives print nothing
return 0;
}