Social Icons

Thursday, 20 February 2014

C# Program to perform Addition,Subtraction,Multiplication & Division in single application



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
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        //for addition
        private void button1_Click_1(object sender, EventArgs e)
        {
            Double a, b, c;
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            c = a + b;
            label3.Text = Convert.ToString(c);
            label3.Show();
            label4.Show();
           
        }

        //for subtraction
        private void button2_Click(object sender, EventArgs e)
        {
            Double a, b, c;
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            c = a - b;
            label3.Text = Convert.ToString(c);
            label3.Show();
            label4.Show();
           
        }

        //for multiplication
        private void button3_Click(object sender, EventArgs e)
        {
            Double a, b, c;
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            c = a * b;
            label3.Text = Convert.ToString(c);
            label3.Show();
            label4.Show();
        }

        //for division
        private void button4_Click(object sender, EventArgs e)
        {
            Double a, b, c;
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            c = a / b;
            label3.Text = Convert.ToString(c);
            label3.Show();
            label4.Show();
        }


        //use only for valid input (eg-not taking character only numbers and dot) 

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46 || e.KeyChar == 8 || e.KeyChar == 9))
                e.Handled = true;
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46 || e.KeyChar == 8 || e.KeyChar == 9))
                e.Handled = true;
        }
   

    }

}
                                                    Output
                       

No comments:

Post a Comment