Creating a Calculator Visual Studio C#
Arithmetic Operators:
Operators are very
useful thing in computing value. While writing a program, you need various
types of operators to calculate value. These operators are categorized as
different categories in C sharp tutorial that performs specific task ex. The C#
arithmetic operator performs basic calculation as add, subtraction,
multiplication, division, and modulus whereas other operators perform different
kind of task. You will learn one by one all these operators in few next
chapters.
Arithmetic Operators:
Operator
|
Description
|
Examples
|
+
|
Add
numbers
|
X=num1+num2
|
–
|
Subtract
numbers
|
X=num1-num2
|
*
|
Multiply
numbers
|
X=num1*num2
|
/
|
Divide
numbers
|
X=num1/num2
|
C#(Code)...............
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
calculater_practice
{
public partial class Form1 : Form
{
double FirstNumber;
string Operation;
public Form1()
{
InitializeComponent();
}
private void n1_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "1";
}
else
{
textBox1.Text = textBox1.Text +
"1";
}
}
private void n2_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "2";
}
else
{
textBox1.Text = textBox1.Text +
"2";
}
}
private void n3_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "3";
}
else
{
textBox1.Text = textBox1.Text +
"3";
}
}
private void n4_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "4";
}
else
{
textBox1.Text = textBox1.Text +
"4";
}
}
private void n5_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "5";
}
else
{
textBox1.Text = textBox1.Text +
"5";
}
}
private void n6_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "6";
}
else
{
textBox1.Text = textBox1.Text +
"6";
}
}
private void n7_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "7";
}
else
{
textBox1.Text = textBox1.Text +
"7";
}
}
private void n8_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "8";
}
else
{
textBox1.Text = textBox1.Text +
"8";
}
}
private void n9_Click(object sender, EventArgs
e)
{
if (textBox1.Text == "0"
&& textBox1.Text != null)
{
textBox1.Text = "9";
}
else
{
textBox1.Text = textBox1.Text +
"9";
}
}
private void n0_Click(object sender, EventArgs
e)
{
textBox1.Text = textBox1.Text + "0";
}
private void Add_Click(object sender, EventArgs
e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "+";
}
private void Sub_Click(object sender, EventArgs
e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "-";
}
private void Mult_Click(object sender, EventArgs
e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "*";
}
private void Div_Click(object sender, EventArgs
e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "/";
}
private void bc_Click(object sender, EventArgs
e)
{
textBox1.Text = "0";
}
private void Form1_Load(object sender, EventArgs
e)
{
}
private void nequal_Click(object
sender, EventArgs e)
{
double secondnumber;
double Result;
secondnumber = Convert.ToDouble(textBox1.Text);
if (Operation == "+")
{
Result = (FirstNumber +
secondnumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "-")
{
Result = (FirstNumber -
secondnumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "*")
{
Result = (FirstNumber *
secondnumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "/")
{
if (secondnumber == 0)
{
textBox1.Text = "connot
divisen by 0";
}
else
{
Result = (FirstNumber /
secondnumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
}
}
private void ndot_Click(object sender, EventArgs
e)
{
textBox1.Text = textBox1.Text + ".";
}
}
}
Comments
Post a Comment