Write a program to print Even number in C# ?
In this program we are going to create one sample program to print even number in C# application
and even number will be display on the screen until it reaches your target number.
There are two possible ways to print even number.
1) Start with number zero, Increment the number by 2, print it , do it until you reach your target number
and this logic will be inside for loop.
2) In For loop Start with number zero, divide the number by 2, if the remainder is zero then number is even number and increment it by 1 and divide the number for consistently by 2. until it reaches the target number
Option 1 :
using System;namespace ConsoleApp{class Program{static void Main(string[] args){Console.WriteLine("Please enter your target");// Declare a variable to hold the target numberint targetNumber = 0;// Retrieve, Convert and store the target numbertargetNumber = Convert.ToInt32(Console.ReadLine());// Use a FOR or WHILE loop to print the even numbers, until our target numberfor (int i = 0; i <= targetNumber; i = i + 2){Console.WriteLine(i);}Console.ReadLine();}}}
Option 2 :
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter your target");
// Declare a variable to hold the target number
int targetNumber = 0;
// Retrieve, Convert and store the target number
targetNumber = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <= targetNumber; i++)
{
if ((i % 2) == 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
}
}