0
0
CsharpProgramBeginner · 2 min read

C# Program to Print Even Numbers

Use a for loop with a condition to check if a number is even using i % 2 == 0, then print it with Console.WriteLine(i).
📋

Examples

InputPrint even numbers from 1 to 5
Output2 4
InputPrint even numbers from 1 to 10
Output2 4 6 8 10
InputPrint even numbers from 2 to 2
Output2
🧠

How to Think About It

To print even numbers, start from the first number in the range and check each number if it divides by 2 with no remainder using the % operator. If yes, print it. Repeat until the end of the range.
📐

Algorithm

1
Set the start and end numbers for the range.
2
Loop from the start number to the end number.
3
For each number, check if it is divisible by 2 with no remainder.
4
If it is even, print the number.
5
Continue until all numbers in the range are checked.
💻

Code

csharp
using System;

class Program
{
    static void Main()
    {
        int start = 1;
        int end = 10;
        for (int i = start; i <= end; i++)
        {
            if (i % 2 == 0)
            {
                Console.WriteLine(i);
            }
        }
    }
}
Output
2 4 6 8 10
🔍

Dry Run

Let's trace printing even numbers from 1 to 10 through the code.

1

Initialize variables

start = 1, end = 10

2

Loop from 1 to 10

i takes values 1, 2, 3, ..., 10

3

Check if i is even

For i=1, 1 % 2 = 1 (odd), skip For i=2, 2 % 2 = 0 (even), print 2 For i=3, 3 % 2 = 1 (odd), skip ... and so on

4

Print even numbers

Print 2, 4, 6, 8, 10

ii % 2 == 0?Action
1falseskip
2trueprint 2
3falseskip
4trueprint 4
5falseskip
6trueprint 6
7falseskip
8trueprint 8
9falseskip
10trueprint 10
💡

Why This Works

Step 1: Loop through numbers

The for loop goes through each number from start to end to check all candidates.

Step 2: Check even condition

Using i % 2 == 0 tests if the number divides evenly by 2, meaning it is even.

Step 3: Print even numbers

Only numbers passing the even check are printed using Console.WriteLine.

🔄

Alternative Approaches

Increment by 2 starting from 2
csharp
using System;

class Program
{
    static void Main()
    {
        for (int i = 2; i <= 10; i += 2)
        {
            Console.WriteLine(i);
        }
    }
}
This skips checking odd numbers and directly prints even numbers, making it more efficient.
Using while loop
csharp
using System;

class Program
{
    static void Main()
    {
        int i = 1;
        while (i <= 10)
        {
            if (i % 2 == 0)
                Console.WriteLine(i);
            i++;
        }
    }
}
This uses a while loop instead of for loop but works similarly.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through all numbers from start to end once, so it runs in linear time O(n).

Space Complexity

It uses a fixed amount of memory regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

Incrementing by 2 is faster because it skips odd numbers, reducing the number of iterations by half.

ApproachTimeSpaceBest For
Check each number with % 2O(n)O(1)Simple and clear logic
Increment by 2 starting at 2O(n/2)O(1)More efficient for large ranges
While loop with % 2 checkO(n)O(1)Alternative loop style
💡
Start your loop at 2 and increment by 2 to print even numbers more efficiently.
⚠️
Forgetting to check the remainder with % 2 == 0 and printing all numbers instead.