Start learning free

LESSON 127 OF 347 · LOOPS & CONDITIONALS

Print Odd Numbers in C#: Fixing the Buggy Loop

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes a number n as argument and prints to the console the odd numbers from 0 through n.

So when we called printOddNumbers(8), we expected our code to print:

1
3
5
7

but it seems like we made some mistakes because when we run our code, it prints nothing.


Assignment:

Your task is to fix our function such that it prints the odd numbers from 0 through n.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · LOOPS & CONDITIONALS

    Buggy Code Print Odd Numbers in C#, explained

    What the code is supposed to do

    PrintOddNumbers(int n) should print the odd numbers below n, one per line. Called with 8, it prints 1, 3, 5 and 7. The starter code runs without any error, but it prints nothing at all, which makes the bug easy to miss: there is no message to read, only an empty output.

    public void PrintOddNumbers(int n) {
        for (int i = 0; i < n; i++) {
            if (n % 2 == 1) {
                Console.WriteLine(n);
            }
        }
    }
    

    What goes wrong

    The loop header is fine. The variable i counts 0, 1, 2 and so on up to 7. The trouble is in the two lines inside the loop, which use n instead of i.

    • n is the method's parameter. It is 8 when the method starts and nothing ever changes it, so it is 8 on every pass.
    • n % 2 == 1 asks whether 8 is odd. The remainder of 8 divided by 2 is 0, so the answer is false on every pass, and Console.WriteLine never runs.

    Even if the check were fixed and the print line were not, the output would be wrong in a different way: the method would print 8 over and over instead of the odd numbers. Both lines need the loop variable.

    Fixing it step by step

    1. Find the value that changes. The loop variable i takes a new value on each pass. The parameter n is only the upper limit.
    2. Fix the check. Test i % 2 == 1, so the condition asks about the current number.
    3. Fix the print. Print i, not n. Otherwise the check and the output would talk about different numbers.

    The fixed code in C#

    public void PrintOddNumbers(int n) {
        for (int i = 0; i < n; i++) {
            if (i % 2 == 1) {
                Console.WriteLine(i);
            }
        }
    }
    
    
    public void MainFunction() {
        PrintOddNumbers(8);
    }
    

    For n = 8, i runs from 0 to 7. The check passes for 1, 3, 5 and 7, and the output is:

    1
    3
    5
    7
    

    A few details about this code:

    • The condition is i < n. The loop stops before reaching n, so n itself is never printed, even when it is odd. PrintOddNumbers(9) prints 1, 3, 5 and 7, not 9.
    • % is the remainder operator. i % 2 is 0 for even numbers and 1 for odd ones, as long as i is not negative. Here i starts at 0 and only grows, so == 1 is safe. For negative values the remainder keeps the sign of the left operand: -3 % 2 is -1, so a check like == 1 would miss negative odd numbers. In code that must handle them, x % 2 != 0 is the safer test.
    • Method names are PascalCase. C# is case-sensitive, so PrintOddNumbers and printOddNumbers are different names. Use the one the lesson's code declares.

    Tracing the fix

    Tracing a few passes by hand is the quickest way to confirm a loop does what you intend. With n = 8:

    • i = 0: 0 % 2 is 0, so nothing is printed.
    • i = 1: 1 % 2 is 1, so 1 is printed.
    • i = 2: 2 % 2 is 0, so nothing is printed.
    • i = 3: 3 % 2 is 1, so 3 is printed.

    The pattern repeats up to i = 7. The same trace on the buggy version shows 8 % 2 being 0 on every pass.

    Common mistakes

    • Using the parameter inside the loop. It compiles, so the compiler can't warn you. The habit that helps: inside a loop, ask which variable changes on each pass.
    • Fixing only one of the two lines. Changing the check to i but still printing n gives 8 printed four times.

    Checking your fix

    Run the code in the editor above. The output should be exactly four lines: 1, 3, 5, 7. Nothing printed means the condition is still false on every pass. Output starting with 0 means the check was changed to == 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