ZeroxInfinity
4/2/2017 - 7:40 AM

C# Check Odd or Even Number

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace checkOddEven
{
    class Program
    {
        static void Main(string[] args)
        {
            int  i; //variable
            Console.Write("Enter a Number : ");//write input
            i = int.Parse(Console.ReadLine()); //read input

            if (i % 2 == 0) // if the remainder of  i (which user inputs) divided by 2 is zero
            {
                Console.Write("Entered Number is an Even Number"); //write output
                
            }
            else
            {
                Console.Write("Entered Number is an Odd Number");
                
            }
        }
    }
}


/*
 * The modulo (%) operator calculates the remainder of a division operation. 
 * In this case, it calculates the remainder of i divided by 2. 
 * If i is an even number, the result will be 0 and if it is an odd number, the result will be 1. 
 * So this if statement checks to see if i is an even number.
 * /

 
/*
 * For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. 
 * For reference types other than string, == returns true if its two operands refer to the same object. 
 * For the string type, == compares the values of the strings.
 */