In this examples teach you how we identifying the number is Even or Odd.
Check if theNumber is Even or Odd using if else Statement in C# Console Application.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static void Main(string[] args) { int n; Console.Write("Enter an integer : "); n = Int32.Parse(Console.ReadLine()); if(n%2==0) { Console.WriteLine("{0} is even",n); } else { Console.WriteLine("{0} is odd", n); } Console.ReadKey(); } |
Example 2 (with static method):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class Program { static bool IsEvenNumber(int num) { if (num % 2 == 0) { return true; } else { return false; } } static void Main(string[] args) { int n; Console.Write("Enter an integer : "); n = Int32.Parse(Console.ReadLine()); if (IsEvenNumber(n)) { Console.WriteLine("{0} is even", n); } else { Console.WriteLine("{0} is odd", n); } Console.ReadKey(); } } |
Output: