You have to use Math.Abs for this. Here i am adding, subtracting and multiplying the two numbers. In Third TextBox i am passing the Operator +,-,*, /. If any others operator is provided then it will give the error.
HTML:
<form id="form1" runat="server">
<div>
X:<asp:TextBox ID="txtXvalue" runat="server" /><br />
Y:<asp:TextBox ID="txtYvalue" runat="server" /><br />
<asp:TextBox ID="txtOp" runat="server" MaxLength="1" />
<asp:Button Text="Calculate" OnClick="Calculate" runat="server" />
<asp:Label ID="lblResult" runat="server" />
</div>
</form>
C#:
protected void Calculate(object sender, EventArgs e)
{
int x = int.Parse(this.txtXvalue.Text.Trim());
int y = int.Parse(this.txtYvalue.Text.Trim());
float z = 0;
string op = this.txtOp.Text.Trim();
switch (op)
{
case "+":
z = x + y;
this.lblResult.Text = (Math.Abs(z)).ToString();
break;
case "-":
z = x - y;
this.lblResult.Text = (Math.Abs(z)).ToString();
break;
case "*":
z = x * y;
this.lblResult.Text = (Math.Abs(z)).ToString();
break;
case "/":
z =(float) x / y;
this.lblResult.Text = (Math.Abs(z)).ToString();
break;
default:
Response.Write("Invalid Operator");
break;
}
}
Thank You.