Press "Enter" to skip to content

Add Two Numbers and Display Sum in C#

Add Two Numbers and Display Sum in C#

Using C#, you can build both desktop and web applications. Console and desktop interface applications are possible to develop with this language.

In this article, contains a simple C# program to add two integers and display their sum. This program is only for learning C# basics.

Here two text boxes will accept two numbers from user and display their sum in another text box.

Interface:

Add Two Numbers and Display Sum in C#

Code behind above interface:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Add_two_numbers
{
    public partial class Form1 : Form
    {
         public Form1()
        {
            InitializeComponent();
        }
        private void Add_button_Click(object sender, EventArgs e)
        {
            try
            {
                double a = Convert.ToDouble(textBox1.Text);
                double b = Convert.ToDouble(textBox2.Text);
                textBox3.Text = (a + b).ToString();
            }
            catch(Exception)
            {
                MessageBox.Show("invalid input");
            }
        }
    }
}

You can customize the code according to your purpose and needs.

More C# programs here

14

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *