Create a simple calculator using C#.net

/*Properties

PROPERTIES
Button1 -> btn1
Button2 -> btn2
Button3 -> btn3
Button4 -> btnplus
Button5-> btnc
Button6 -> btn4
Button7 -> btn5
Button8 -> btn6
Button9 -> btnminus
Button10 -> btnprod
Button11 -> btn7
Button12 -> btn8
Button13 -> btn9
Button14 ->btnmul
Button15 -> btndot
Button16 -> btn0
Button17 -> btnplusorminus
Button18 -> btndiv
Button19 -> btnequals

TEXTBOX PROPERTIES
TextBox Name -> display
VRIABLES
String opr;
Double oparand1,oparand2,result;
*/

// Start coding….
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 WindowsFormsApplication9
{
public partial class Calculator : Form
{
string opr;
double oparand1, oparand2, result;
double SqrRoot;
public Calculator()
{
InitializeComponent();
}

private void btn1_Click(object sender, EventArgs e)
{
display.Text = display.Text + “1”;
}

private void btn2_Click(object sender, EventArgs e)
{
display.Text = display.Text + “2”;
}

private void btn3_Click(object sender, EventArgs e)
{
display.Text = display.Text + “3”;
}

private void btn4_Click(object sender, EventArgs e)
{
display.Text = display.Text + “4”;
}

private void btn5_Click(object sender, EventArgs e)
{
display.Text = display.Text + “5”;
}

private void btn6_Click(object sender, EventArgs e)
{
display.Text = display.Text + “6”;
}

private void btn7_Click(object sender, EventArgs e)
{
display.Text = display.Text + “7”;
}

private void btn8_Click(object sender, EventArgs e)
{
display.Text = display.Text + “8”;
}

private void btn9_Click(object sender, EventArgs e)
{
display.Text = display.Text + “9”;
}

private void btn0_Click(object sender, EventArgs e)
{
display.Text = display.Text + “0”;
}

private void btnc_Click(object sender, EventArgs e)
{
display.Clear();
}

private void btnplus_Click(object sender, EventArgs e)
{
oparand1 = Convert.ToDouble(display.Text);
opr = “+”;
display.Clear();
}

private void btnminus_Click(object sender, EventArgs e)
{
oparand1 = Convert.ToDouble(display.Text);
opr = “-“;
display.Clear();
}

private void btnmul_Click(object sender, EventArgs e)
{
oparand1 = Convert.ToDouble(display.Text);
opr = “*”;
display.Clear();
}

private void btndiv_Click(object sender, EventArgs e)
{
oparand1 = Convert.ToDouble(display.Text);
opr = “/”;
display.Clear();
}

private void btnprod_Click(object sender, EventArgs e)
{
oparand1 = Convert.ToDouble(display.Text);
opr = “%”;
display.Clear();
}

private void btndot_Click(object sender, EventArgs e)
{
if (display.Text.Contains(“.”))
{
display.Text = display.Text;
}
else
{
display.Text = display.Text + “.”;
}

}

private void btnplusorminus_Click(object sender, EventArgs e)
{
if (display.Text.Contains(“-“))
{
display.Text = display.Text.Remove(0, 1);
}
else
{
display.Text = “-” + display.Text;
}
}

private void btnequals_Click(object sender, EventArgs e)
{
oparand2 = Convert.ToDouble(display.Text);
switch (opr)
{
case “+”: result = oparand1 + oparand2;
display.Text = Convert.ToString(result);
break;

case “-“: result = oparand1 – oparand2;
display.Text = Convert.ToString(result);
break;
case “*”: result = oparand1 * oparand2;
display.Text = Convert.ToString(result);
break;
case “/”: if (oparand2 == 0)
{
display.Text = “0.0”;
break;
}
else
{
result = oparand1 / oparand2;
display.Text = Convert.ToString(result);
break;
}
case “%”: result = oparand1 % oparand2;
display.Text = Convert.ToString(result);
break;

}
}

}

Design View shown below…

This slideshow requires JavaScript.

Calculator

Download Calculator