CSharp - Checking whether we can parse the string as an integer

Description

Checking whether we can parse the string as an integer

Demo

using System;

class Program/*w w  w  . j  av  a2 s  .c o m*/
{
    static void Main(string[] args)
    {
        string input;
        int number;
        Console.WriteLine("Enter a number");
        input = Console.ReadLine();
        //Checking whether we can parse the string as an integer
        if (int.TryParse(input, out number))
        {
            Console.WriteLine("You have entered {0}", input);
        }
        else
        {
            Console.WriteLine("Invalid input");
        }
        if (number % 2 == 0)
        {
            Console.WriteLine("{0} is an even number", number);
        }
        else
        {
            Console.WriteLine("{0} is an odd number", number);
        }
    }
}

Result

Related Topic