Start learning free

LESSON 126 OF 347 · LOOPS & CONDITIONALS

Print Even and Odd in C#: for Loop with if and else

Practice Read the full explanation

Given a positive integer n, for each number from 0 to n - 1, print "odd" if the number is odd or the number itself if it is even.

Example:

Input: n = 6
Output (console):
0
odd
2
odd
4
odd


Note:

You have to print to the console, your function shouldn't return anything.

STEP 1 OF 3
    Ctrl + Enter to check

    C# · LOOPS & CONDITIONALS

    Print Even Odd in C#, explained

    Problem overview

    The method PrintEvenOdd(int n) looks at each number from 0 to n - 1. For an odd number it prints the word odd. For an even number it prints the number itself. It returns nothing, because the result is the console output.

    For n = 6 the numbers checked are 0, 1, 2, 3, 4 and 5. The output is:

    0
    odd
    2
    odd
    4
    odd
    

    The number n itself is not included: the range stops one before it.

    Approach

    Every number gets one of two treatments, so the plan is a loop over the range with an if/else inside it. The loop count is known in advance (n numbers), which makes a for loop the natural choice.

    The only real question is how to tell odd from even. The remainder operator % answers it: i % 2 is the remainder of dividing i by 2. It is 0 for even numbers and 1 for odd ones, as long as i is not negative. Here i starts at 0 and only increases, so the remainder is always 0 or 1.

    Step by step

    1. Write the loop. A variable i starts at 0 and runs while i < n, which covers 0 up to n - 1. The header uses i++ to move on by one.
    2. Handle the odd case. Inside the loop, test i % 2 == 1. If it is true, print the string "odd". Here the quotes are needed, because the output is the word itself.
    3. Handle the other case. Add an else branch that prints i, without quotes, so that the number is written. Everything that is not odd is even, so no second test is needed.

    The solution in C#

    class Solution {
        public void PrintEvenOdd(int n) {
            for (int i = 0; i < n; i++) {
                if (i % 2 == 1) {
                    Console.WriteLine("odd");
                } else {
                    Console.WriteLine(i);
                }
            }
        }
    }
    

    Some details worth knowing:

    • Console.WriteLine has versions for strings and for integers. In the first branch the argument is a string; in the second it is an int, which is converted to text when written.
    • Only one branch runs per pass, so each number produces exactly one line.
    • The method is void. It prints, and it does not build or return a list.
    • The method name PrintEvenOdd and the parameter n must match exactly, since C# is case-sensitive.

    Complexity

    The loop runs n times and each pass does a constant amount of work, so the time is O(n). Only the counter is stored, so the space is O(1).

    Edge cases and common mistakes

    • Zero is even. 0 % 2 is 0, so the first line of output is the number 0, as in the example.
    • Using <= in the loop. i <= n adds an extra line for n itself.
    • Quotes around i. Console.WriteLine("i") prints the letter i every time.
    • Swapping the branches. Printing the number for odd values and the word for even values reverses the output. Check the example: index 0 shows a number, index 1 shows odd.
    • = instead of ==. if (i % 2 = 1) does not compile in C#.
    • n = 0. The loop does not run, and nothing is printed.
    • Negative numbers. The task does not include them, but note that in C# -3 % 2 is -1, so a test of == 1 would miss negative odd numbers. Testing i % 2 == 0 for evenness is safe for every integer.

    Testing

    • n = 6 prints 0, odd, 2, odd, 4, odd.
    • n = 1 prints 0.
    • n = 2 prints 0 and odd.
    • n = 0 prints nothing.
    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