Start learning free

LESSON 72 OF 409 · CONDITIONAL STATEMENTS

Return the Negative of a Number in C++ with if and else

Practice Read the full explanation

Create a function that takes a number as an argument and returns negative of that number. Return negative numbers without any change.

Examples:

4 ➞ -4

15 ➞ -15

-4 ➞ -4

0 ➞ 0

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is not negative.
STEP 1 OF 3
    Ctrl + Enter to check

    C++ · CONDITIONAL STATEMENTS

    Return Negative in C++, explained

    Problem overview

    The method negative(int n) must return the negative of n. A number that is already negative is returned unchanged. Zero stays zero.

    negative(4)   gives -4
    negative(15)  gives -15
    negative(-4)  gives -4
    negative(0)   gives 0
    

    So the result is never positive: negative(4) flips the sign, and negative(-4) keeps it. This is different from simply writing -n, which would turn -4 into 4.

    Approach: decide, then flip or keep

    The task has two cases, and the sign of n tells you which one applies. A conditional statement chooses between them:

    • If n < 0, it is already negative, so return it as it is.
    • Otherwise n is zero or positive, and the unary minus -n gives the negative value. For zero, -0 is just 0.

    Zero needs no case of its own. The test n < 0 is false for 0, and -n returns 0, which is what the examples ask for.

    Put an input through both paths. For n = 15, the test 15 < 0 is false, so the code takes the else and returns -15. For n = -4, the test -4 < 0 is true, so the code returns -4 and never reaches the else.

    Step by step

    1. Write an if that checks n < 0. A true condition means the input is already negative.
    2. Return n inside the if. Nothing has to be changed. Returning leaves the method immediately.
    3. Write an else that returns -n. It runs for zero and every positive number. The unary minus negates the value.

    The solution in C++

    class Solution {
    public:
        int negative(int n) {
            if (n < 0) {
                return n;
            } else {
                return -n;
            }
        }
    };
    

    Both branches return, so the method always produces a value. Compilers can see this and do not warn about reaching the end without a return. Because each branch ends in return, the else is not strictly needed: writing if (n < 0) { return n; } followed by return -n; behaves the same. Keeping the else makes the two cases easy to see.

    The same logic fits in one line with the conditional operator: return n < 0 ? n : -n;. Another form is return -abs(n); with <cstdlib>, but the lesson practises if and else.

    Complexity

    Skipped for such a short lesson: the method does one comparison and at most one negation, so time and space are both constant.

    Edge cases and common mistakes

    • Zero. negative(0) returns 0, through the else branch. Writing n <= 0 in the condition would also work, since both branches then return 0.
    • Returning -n for everything. That turns -4 into 4. The check for negative input is what stops it.
    • Writing n = -n; and forgetting to return it. Assigning to the parameter changes only the local copy. The method must return the value.
    • Using = in the condition. if (n = 0) assigns 0 to n and tests the result. Use <, <= or == to compare.
    • The smallest int. INT_MIN is already negative, so it returns through the first branch. For positive inputs, -n never overflows, because the positive range is smaller than the negative one by a single value. The one value to avoid is negating INT_MIN itself, which is undefined behaviour in C++, and this method never does.

    Testing

    • negative(4) gives -4.
    • negative(15) gives -15.
    • negative(-4) gives -4.
    • negative(0) gives 0.
    • negative(2147483647) gives -2147483647.
    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free