 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Basic calculator program using C#
To create a calculator program in C#, you need to use Web Forms. Under that create buttons from 1-9, addition, subtraction, multiplication, etc.
Let us see the code for addition, subtraction, and multiplication. Firstly, we have declared two variables −
static float x, y;
Now, we will see how to set codes for calculation on individual button click: Our result textbox is tbResult since we have used Windows Form as well to display the calculator −
protected void add_Click(object sender, EventArgs e) {    x = Convert.ToInt32(tbResult.Text);    tbResult.Text = "";    y = '+';    tbResult.Text += y; } protected void sub_Click(object sender, EventArgs e) {    x = Convert.ToInt32(tbResult.Text);    tbResult.Text = "";    y = '-';    tbResult.Text += y; } protected void mul_Click(object sender, EventArgs e) {    x = Convert.ToInt32(tbResult.Text);    tbResult.Text = "";    y = '*';    tbResult.Text += y; } The following is the equal button code −
protected void eql_Click(object sender, EventArgs e) {    z = Convert.ToInt32(tbResult.Text);    tbResult.Text = "";    if (y == '/') {       p = x / z;       tbResult.Text += p;       x = d;    } else if (y == '+') {       p = x + z;       tbResult.Text += p;       a = d;    } else if (y == '-') {       p = x - z;       tbResult.Text += p;       x = p;    } else {       p = x * z;       tbResult.Text += p;       x = p;    } }Advertisements
 