Basic Calculator in c#


HI Programmers,
Today we are going to create a small basic calculator in C#. This calculator will perform operations like Addition,Subtraction,Multiplication and Division. This is basically a Windows Application. I choose to use windows Application over the Console Application for this program because it is a User Interface and also looks good and interesting to use. Below is the C# Window Application for calculator program.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Windows_Form_Activities
{
     public partial class Form1 : Form
     {
      int result;
      public Form1()
      {
       InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
       if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
       {
        MessageBox.Show("Please enter value first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
       }
       else
       {
        result = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);
        MessageBox.Show("Addition is " + result);
       }
      }
      private void sub_Click(object sender, EventArgs e)
      {
       if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
       {
        MessageBox.Show("Please enter value first","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
        return;
       }
       else
       {
       result = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);
       MessageBox.Show("Subtraction is " + result);
       }
}
        private void mul_Click(object sender, EventArgs e)
        {
        if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
        {
         MessageBox.Show("Please enter value first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;
        }
        else
        {
         result = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);
         MessageBox.Show("Multiplication is " + result);
        }
      }
      private void div_Click(object sender, EventArgs e)
      {
        if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
        {
         MessageBox.Show("Please enter value first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;
        }
        else
        {
         result = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
         MessageBox.Show("Division is " + result);
        }
      }
     }
}






Explaination
  • The above application program calculator performs four basic operations like addition,subtraction,Multiplication and division.
  • We have taken two TextBoxes and Four Buttons for four different operations respectively.
  • The User have to enter two numbers in two Textboxes respectively.
  • After entering numbers , he has to click on button of which he wants to perform operations.
  • The operations will be performed and the result will be appeared as messagebox to user.
  • From the beginning , the user first need to deisgn UI for the application and then need to write code.
  • There are four events for each operation
  • You don't need to write events separately. You just double click on button in design and that event code will be automatically generated.
  • In above program, We first checked whether numbers are inserted in two textboxes or not. We perform that validation using IF statement.
  • Then We perform the operation based on the Button clicked.
  • Basic Calculator in c# Basic Calculator in c# Reviewed by LanguageExpert on March 17, 2018 Rating: 5

    No comments