Calculate Sum, Multiply, Division Of Two Numbers And Display Current Date In C#.NET

  1. First, add new Website, Open Visual Studio: Select > File > New > Website.

  2. Give the name of the Website like SampleWeb1.

  3. Open Solution Explorer and add Web Form by right clickng on the Website name > select Add > Add New Item >Select Web form (Default.aspx).

  4. Add Label on Default.aspx,

    Eg: <asp:Label ID="Label1" runat="server" />

  5. Open Default.aspx.cs page and add following code on Page Load,
    1. protected void Page_Load(object sender, EventArgs e)  
    2. {  
    3.     if (Page.IsPostBack == false) {  
    4.         Label1.Text = DateTime.Now.ToString();  
    5.     }  
    6. }  

Now, we learn how to calculate the sum, multiplication, and division of 2 numbers:

  1. Add three textboxes and three buttons on default.aspx page,

    Value1: <asp:TextBox ID="TextBox1" runat="server" />

    Value2: <asp:TextBox ID="TextBox2" runat="server" />

    Answer
    1. <asp:TextBox ID="TextBox3" runat="server" />  
    2. <asp:Button ID="Button1" Text="Sum" runat="server" OnClick="calculate" />  
    3. <asp:Button ID="Button2" Text="Mul" runat="server" OnClick="calculate" />  
    4. <asp:Button ID="Button3" Text="Divide" runat="server" OnClick="calculate" />  
  2. Add Following code in default.aspx.cs Page,
    1. protected void calculate(object sender, EventArgs e)  
    2. {  
    3.     if (sender == Button1) {  
    4.         TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text));  
    5.     } else if (sender == Button2) {  
    6.         TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(TextBox2.Text));  
    7.     } else {  
    8.         TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox2.Text));  
    9.     }  
    10. }