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.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

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();

        }

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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();

        }

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

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. Caltech CTME and IIT-Kanpur collaborate in teaching our Software Development Courses. These 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.