Test for Even or Odd numbers in asp.net.   

Odd numbers can never be Even and vice- versa. With modulo division, we can see if the number is divisible by 2 and if the remainder is 0 - then it is an even number, else it is an odd number.

0 530 Technology | Asp.Net
Tue Dec 25 , 2018

In this article we will check whether the number entered in textbox is even or odd.

Example

Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center;margin-top: 20%;">
    <asp:Label ID="Label1" runat="server"></asp:Label> 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="btnEvenOdd" runat="server" onclick="btnEvenOdd_Click"/>
     <br /><br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

 
       protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Enter Number";

            btnEvenOdd.Text = "Number is Even or Odd";
            Label2.Visible = false;
        }
        protected void btnEvenOdd_Click(object sender, EventArgs e)
        {
            int num;
            num = Convert.ToInt32(TextBox1.Text);
            Label2.Visible = true;

            if (num % 2 == 0)
            {
                Label2.Text = num + " is even number";
            }
            else
            {
                Label2.Text = num + " is odd number";
            }
        }



Subscribe & Get Regular Updates On Your E-mail