Start learning free

LESSON 70 OF 347 · CONDITIONAL STATEMENTS

Return Negative in C#: Using if-else and Unary Minus

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:

negative(4) ➞ -4

negative(15) ➞ -15

negative(-4) ➞ -4

negative(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 positive.
STEP 1 OF 3
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    Return Negative in C#, explained

    Problem overview

    You write a method Negative that takes an integer n and returns its negative version. A positive number becomes negative, a number that is already negative stays as it is, and zero stays zero.

    nReturned
    4-4
    15-15
    -4-4
    00

    The point is that the result is never positive. A common first guess is to always flip the sign, but that would turn -4 into 4, which breaks the rule that negatives come back unchanged.

    Approach

    Split the input into two groups. If n is already below zero, return it as it is. Otherwise, n is zero or positive, and the unary minus -n gives the value you want. For zero, -0 is just 0, so zero needs no special case.

    Unlike a method that prints, this one hands a value back to the caller, so each branch uses return.

    A quick way to check the idea is to trace three inputs. For -4 the test passes and the method returns -4. For 4 the test fails, the else branch runs, and the method returns -4. For 0 the test fails too, and -0 comes out as 0. All three match the table.

    Step by step

    1. Test for negative input. Write if (n < 0) with an empty block first. That condition means the input is already in its final form.
    2. Return it unchanged. Inside the block, return n;. The method ends right there.
    3. Return the flipped value otherwise. The else block returns -n. The minus sign in front of a variable is the unary minus operator: it negates the value. For 4 it gives -4, and for 0 it gives 0.

    The solution in C#

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

    C# details

    • The return type is int, so every path through the method has to return an int. C# checks this at compile time. Here both branches return, so the compiler knows no path is left without a value. If you removed the else block and its return, the method would fail to compile with an error saying that not all code paths return a value.
    • The method is named Negative, with a capital letter, and it is an instance method of Solution. The examples in the problem write it in lowercase, but the code you write has to use the name from the editor.
    • Zero is handled by the else branch. -0 on an integer is plain 0, because integers have no separate negative zero.

    The next lesson shows that the else here can be removed without changing the result. Keep it for now, because it makes the two cases explicit.

    Edge cases and common mistakes

    • Negating negatives. Writing return -n; unconditionally turns -4 into 4. The if exists to prevent that.
    • Zero. Both n < 0 and n <= 0 give the right answer for zero here, since -0 and 0 are the same integer. The lesson uses n < 0.
    • Extreme values. The smallest int, -2147483648, is negative, so it is returned unchanged. The risky direction would be negating it, which cannot be represented as an int. C# wraps silently in that case rather than throwing, but this solution never negates it.
    • Using Math.Abs without thinking. -Math.Abs(n) also solves the problem in one line, but the lesson is practising the if-else form. Note that Math.Abs(int.MinValue) throws an OverflowException, so the explicit branches are safer.

    Testing

    Call the method with these values:

    • Negative(4) returns -4.
    • Negative(15) returns -15.
    • Negative(-4) returns -4.
    • Negative(0) returns 0.
    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