# Example 3

Generate first hundred even numbers.

What is an even number?

Any number than can be divided exactly by 2 is an even number. 0, 2, 4, 6, 8, ... are even numbers.

 




for (int i = 0; i < 200; i += 2)
{
  Console.WriteLine(i);
}
1
2
3
4

Notice that in line 1, we start the loop from 0 and finish it at 198. We increment the loop variable by 2 so that we keep getting even numbers.

If you do not want to increment the loop variable i by 2, then you can do this,

 

 





for (int i = 0; i < 200; i++)
{
  if (i % 2 == 0)
  {
    Console.WriteLine(i);
  }
}
1
2
3
4
5
6
7

Here we,

  1. generate numbers from 1 to 200
  2. if the number is an even then we print it, otherwise we ignore it
Last Updated: Apr 23, 2020, 7:17 PM