Friday, 27 December 2013

How to check User Input Number is Even or Odd without using conditional statement C#


using System;

class Program
{
    public static void Main()
    {
        try
        {
            Console.WriteLine("Enter any number : ");
            int number = Convert.ToInt32(Console.ReadLine());

            int remainder = number % 2;
            int checkNumber = number / remainder;

// if remainder is not zero than this statement will execute other wise program control goes to catch block bcoz divide by zero exception would be occured
            Console.WriteLine("Number is Odd");
            Console.ReadKey();
        }

        catch (Exception ex)
        {
            Console.WriteLine("Number is even");
            Console.ReadKey();
        }
    }
}
To implement same concept in Java Click Here


That’s it!!…..Happy Programming...

4 comments: