Start learning free

LESSON 68 OF 347 · CONDITIONAL STATEMENTS

Is Number Negative in C#: Printing with if and else

Practice Read the full explanation

Create a function that takes a number n as its only argument and prints "negative" to the console if n is less than zero, otherwise prints "positive" to the console.

Example 1:

Input: n = 5
Output: "positive"
Explanation:
5 is greater than or equal to zero, so our function prints "positive" to the console.

Example 2:

Input: n = 0
Output: "positive"
Explanation:
0 is greater than or equal to zero, so our function prints "positive" to the console.

Example 3:

Input: n = -2
Output: "negative"
Explanation:
-2 is less than zero, so our function prints "negative" to the console.

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

    Is Number Negative in C#, explained

    Problem overview

    You write a method IsNegative that takes an integer n and prints one word to the console. It prints negative when n is less than zero, and positive for everything else, including zero. The method does not return anything: its result is the text it prints.

    nPrintedWhy
    5positive5 is not less than 0
    0positive0 is not less than 0
    -2negative-2 is less than 0

    Zero is the case to watch. Mathematically zero is neither positive nor negative, but this problem puts it in the "positive" group, because the rule is "negative if below zero, otherwise positive".

    Approach

    There are exactly two outcomes, and one test separates them: is n < 0? If the answer is yes, print negative. If the answer is no, print positive. That is the shape of an if-else statement, so there is no need for a second condition.

    Testing for "less than zero" rather than "greater than zero" is what handles zero correctly. Since 0 < 0 is false, zero lands in the else branch automatically.

    Compare this with the alternative of two independent checks, one for n < 0 and one for n >= 0. It works, but it asks the same question twice and relies on you keeping the two conditions exactly opposite. With else, the second branch is defined as "whatever the first test rejected", so the two can never drift apart.

    Step by step

    1. Write the condition. Start with if (n < 0) and an empty block. The parentheses are required, and the condition has to be a bool, which a comparison always produces.
    2. Handle the negative case. Inside the block, call Console.WriteLine("negative");. WriteLine prints the text and ends the line.
    3. Handle the other case. Add else after the closing brace and print "positive" in its block. The else runs for every n that failed the test, which means zero and all positive numbers.

    The solution in C#

    class Solution {
        public void IsNegative(int n) {
            if (n < 0) {
                Console.WriteLine("negative");
            } else {
                Console.WriteLine("positive");
            }
        }
    }
    

    C# details

    • void in the signature means the method returns no value. Nothing here uses return, because the output happens through Console.WriteLine.
    • The method name is IsNegative with a capital I. C# is case-sensitive, so a method called isNegative would be a different name, and calling it would fail to compile.
    • The strings must match exactly, including lowercase letters: "Negative" is not the same text as "negative".
    • The type int only holds whole numbers, so you never have to think about decimals in this method.

    Reading it as the program runs

    For n = -2, C# evaluates -2 < 0, gets true, prints negative and jumps past the else block. For n = 0, it evaluates 0 < 0, gets false, skips the first block and prints positive. Tracing a couple of inputs like this is a good habit for checking conditions.

    Edge cases and common mistakes

    • Zero. Testing n <= 0 would print negative for zero, which is wrong here. Testing n > 0 for positive and everything else for negative would also get zero wrong.
    • Printing twice. If you write two separate if statements and make a mistake in the conditions, both or neither could print. The if-else form guarantees exactly one line of output.
    • Returning instead of printing. The method is void, so return "negative"; will not compile. The answer has to go to the console.
    • Using = instead of ==. This problem needs only <, but if you compare to a specific value later, remember that = assigns and == compares.

    Testing

    Try these inputs in the editor above:

    • 5 prints positive.
    • 0 prints positive.
    • -2 prints negative.
    • -1 prints negative, the closest negative integer to the boundary.
    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