
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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; } }
- Related Articles
- Basic calculator program using Java
- Basic calculator program using Python
- Basic calculator program using Python program
- Basic Calculator in C++
- Basic Calculator II in Python
- Basic Calculator III in C++
- Golang Program to create a Class that can perform basic Calculator Operations
- Java program to generate a calculator using the switch case
- How to write a simple calculator program using C language?
- Java Program to Make a Simple Calculator Using switch...case
- Golang Program to make a Simple Calculator using Switch Case
- Haskell Program to Make a Simple Calculator Using switch...case
- Program for EMI Calculator in C program
- Average Speed Calculator using Tkinter
- Ratio Calculator GUI using Tkinter

Advertisements