RE: [C#] Even number 11-28-2021, 06:36 PM
#9
and here is the same modulus operator (%) and using a ternary to replace the if/else. It's a nice way to refactor lines and to get alternating row colors, turning something on and off, or even counting binary bits.
Code:
/// sunjester 2021
/// OddEvenTernary.cs
using System;
namespace OddEvenTernary
{
  class OddEvenTernary
  {   Â
    static void Main(string[] args)
    {
      String oddEven = String.Empty;
      for(int i=0;i<=10;i++)
      {
        oddEven = (i%2==1 ? "Odd" : "Even");
        Console.WriteLine(i + " is " + oddEven);
      }
    }
  }
}Code:
c:\software>csc OddEvenTernary.cs && OddEvenTernary.exe
Microsoft (R) Visual C# Compiler version 3.11.0-4.21403.6 (ae1fff34)
Copyright (C) Microsoft Corporation. All rights reserved.
0 is Even
1 is Odd
2 is Even
3 is Odd
4 is Even
5 is Odd
6 is Even
7 is Odd
8 is Even
9 is Odd
10 is Even


![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)