A One-Stop Solution for Creating a Basic Calculator in C#

Nowadays, most calculators are powered by electricity. Engineers and students prefer battery-operated calculators. Before 1970, the slide rule, a cruder calculator, was widely used. It consisted of a slide, a wooden slat that could be slid between two reinforced slats. The slide and the outside slats had calibrated numerical scales.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Project Creation for Calculator in C# In Visual Studio

You will use visual studio 2022 for this project. You will follow the following steps to create a project:

Calculator_in_C#-vs2022-img2.

  • You must click on “Create a New Project.”

Calculator_in_C#-vs2022-img3

  • Then you must search “Windows Forms App (.NET Framework).”

Calculator_in_C#-vs2022-img4

  • Then you must give this project a name like “calculator demo.”

Now, work on the GUI of the calculator.

Unleash Your Career as a Full Stack Developer!

Full Stack Developer - MERN StackEXPLORE COURSE
Unleash Your Career as a Full Stack Developer!

GUI Creation of Calculator in C#

Calculator_in_C#-GUI-img1

  • You must change the window label from form1 to the “Basic Calculator.”
  • Then you should start by adding some labels to understand the GUI.
  • You can find the label in the toolbox. To access the toolbox go to view and select toolbox.
  • The first label is a calculator in size 20, blue.
  • Then add three more labels for value 1, value 2, and the output. 
  • You should add some text boxes to take input and show output according to the arithmetic operations you will be performing.
  • You must add some buttons to do operations like addition, subtraction, multiplication, division, and percentage.

Now, you will write a code for the arithmetic function.

Also Read: 4 Free and Low-Cost Online Resources for Practicing Code

Button Click Functionality Code for Calculator in C#

Calculator_in_C#-code-img1

You must write code for each button separately.

1. You Must Start With the Addition (+) Button:

 private void button1_Click(object sender, EventArgs e)

        {

/* we will take the textbox’s value for value 1 and 2 as double and store it in txt1 and txt2 */

       double txt1 = Convert.ToDouble(textBox1.Text); 

  double txt2 = Convert.ToDouble(textBox2.Text);

       double sum = txt1 + txt2;

     //Now to display the sum in textbox3. 

//But we will convert this value to string 

//because textbox only recognizes strings. 

textBox3.Text = sum.ToString();

        }

2. You Should Write the Code for the Subtraction (-) Button:

 private void button2_Click(object sender, EventArgs e)

        {

            double txt1 = Convert.ToDouble(textBox1.Text);

            double txt2 = Convert.ToDouble(textBox2.Text);

            double diff = txt1 - txt2;

            textBox3.Text = diff.ToString();

        }

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

3. You Must Write Up the Code for the Division (/) Button:

 private void button3_Click(object sender, EventArgs e)

        {

            double txt1 = Convert.ToDouble(textBox1.Text);

            double txt2 = Convert.ToDouble(textBox2.Text);

            double division = txt1 / txt2;

            textBox3.Text = division.ToString();

        }

4. Write the Code for the Multiplication (*) Button:

 private void button4_Click(object sender, EventArgs e)

        {

            double txt1 = Convert.ToDouble(textBox1.Text);

            double txt2 = Convert.ToDouble(textBox2.Text);

           double product = txt1 * txt2;

            textBox3.Text = product.ToString();

        }

5. You Should Write the Code for the Percentage (%) Button:

private void button5_Click(object sender, EventArgs e)

        {

            double txt1 = Convert.ToDouble(textBox1.Text);

            double txt2 = Convert.ToDouble(textBox2.Text);

            double percent = (txt1 / txt2)*100;

            textBox3.Text = percent.ToString();

        }

Next Steps

"C# Arraylists" can be your next concept to learn. ArrayList is an ordered collection of objects that can be individually indexed. It is essentially an array substitute. It also supports dynamic memory allocation and adds searches and sorts items in a list.

You've come to the right place if you want to learn more about software development and possibly work in it. Our Full Stack Developer - MERN Stack classes cover data structures and algorithms from fundamentals to advanced concepts like Competitive Programming. You'll learn about data structures like trees, graphs, and queues as a software developer. 

If you have any queries about this "Calculator in C#" tutorial, please drop them in the comment section below. Our 24/7 expert team will promptly respond to your queries.

About the Author

Kartik MenonKartik Menon

Kartik is an experienced content strategist and an accomplished technology marketing specialist passionate about designing engaging user experiences with integrated marketing and communication solutions.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.