What the code is supposed to do
The method PrintEvenNumbers(int[] nums) should print the even numbers of the array, each on its own line. For new int[]{2, 1, 0, 4, 3}, the expected output is 2, 0 and 4, in that order. Zero counts as even, because dividing it by 2 leaves no remainder.
The code in the editor produces the word even twice instead. That output hints at what is wrong: the program prints a fixed piece of text, and it prints it for the wrong numbers. There are two separate bugs, and both are inside the loop.
What goes wrong
public void PrintEvenNumbers(int[] nums) {
foreach (int num in nums) {
if (num % 2 == 1) {
Console.WriteLine("even");
}
}
}
Bug 1: the condition selects odd numbers
The % operator gives the remainder of a division. For an even number the remainder after dividing by 2 is 0, and for an odd number it is 1. The condition num % 2 == 1 is therefore true for odd numbers. In the array 2, 1, 0, 4, 3, the odd numbers are 1 and 3, which is exactly why the output has two lines.
The fix is to test for a remainder of 0: num % 2 == 0.
Bug 2: it prints text, not the number
Console.WriteLine("even") prints the characters e-v-e-n, exactly as written inside the quotes, every time it runs. The task wants the number itself. Removing the quotes and passing the variable, Console.WriteLine(num), prints the value that num holds on that pass.
The fixed code
With both changes, the loop keeps the numbers whose remainder is 0 and prints them:
public void PrintEvenNumbers(int[] nums) {
foreach (int num in nums) {
if (num % 2 == 0) {
Console.WriteLine(num);
}
}
}
public void MainFunction() {
PrintEvenNumbers(new int[]{2, 1, 0, 4, 3});
}
Tracing the call shows why it works:
num | num % 2 | Printed? |
|---|---|---|
| 2 | 0 | yes |
| 1 | 1 | no |
| 0 | 0 | yes |
| 4 | 0 | yes |
| 3 | 1 | no |
The output is 2, 0 and 4. The MainFunction part is the lesson's own entry point and stays as it is.
Why test for 0 and not for 1
It is tempting to treat "odd" as the opposite of "even" and test == 1, but that only works for non-negative numbers. In C#, the remainder takes the sign of the left operand, so -3 % 2 is -1, not 1. A test for even numbers, num % 2 == 0, is correct for negative numbers too, since -4 % 2 is 0. If you ever need to test for odd numbers, num % 2 != 0 is the safe form.
How to find bugs like these
Run the code by hand on the sample input and compare each line of output with what you expected. Here the expected output has three lines and the actual output has two, so the loop is picking the wrong elements. The expected lines are numbers and the actual lines are words, so the print statement is wrong as well. Counting the lines and reading their content separately is a quick way to tell a selection bug from a printing bug.
Common mistakes
- Fixing only one bug. Changing the condition alone prints the word
eventhree times, once for each even number. Changing the print alone prints the odd numbers 1 and 3. - Using
=instead of==.num % 2 = 0is an assignment, and it does not compile. - Printing with
Console.Write. It does not end the line, so the output would be204on a single line. - Skipping zero. Zero is even, and the expected output includes it.
Key takeaways
num % 2 == 0is the test for an even number.- Text in quotes is printed literally; a variable name without quotes prints its value.
- When a program prints the wrong thing, check both what it selects and what it prints.