Creating a Simple Calculator in C#
In this tutorial, we will build a simple console-based calculator application in C#. This program will allow users to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. By the end of this tutorial, you will have a better understanding of user input, conditional statements, and methods in C#.
Setting Up the Project
Start by creating a new console application project in Visual Studio:
- Open Visual Studio and select Create a new project.
- Choose Console App (.NET Core) or Console App (.NET Framework) and click Next.
- Name your project "SimpleCalculator" and click Create.
Writing the Calculator Code
Now, let's write the code for our calculator. Open the Program.cs
file and replace the existing code with the following:
using System;
namespace SimpleCalculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Simple Calculator!");
Console.WriteLine("Choose an operation:");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
int operation = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
double result = 0;
switch (operation)
{
case 1:
result = Add(num1, num2);
break;
case 2:
result = Subtract(num1, num2);
break;
case 3:
result = Multiply(num1, num2);
break;
case 4:
result = Divide(num1, num2);
break;
default:
Console.WriteLine("Invalid operation.");
return;
}
Console.WriteLine("The result is: " + result);
}
static double Add(double a, double b) => a + b;
static double Subtract(double a, double b) => a - b;
static double Multiply(double a, double b) => a * b;
static double Divide(double a, double b) => a / b;
}
}
Understanding the Code
Let's go through the key components of the calculator program:
using System;
: This line imports the System namespace, allowing us to use common classes and methods.static void Main(string[] args)
: This is the entry point of the application where the program execution begins.Console.WriteLine()
: This method is used to display text to the console.Convert.ToInt32()
andConvert.ToDouble()
: These methods convert user input from strings to numeric types.switch (operation)
: This statement evaluates the user's choice and executes the corresponding arithmetic operation.- Arithmetic methods:
Add
,Subtract
,Multiply
, andDivide
are methods that perform calculations and return results.
Compiling and Running the Calculator
To compile and run your calculator application, follow these steps:
- Press
F5
or click the Start button in Visual Studio to build and run your program. - Follow the prompts in the console window to select an operation and enter two numbers.
- The calculator will display the result of the operation you selected.
Conclusion
You have successfully created a simple calculator application in C#. This tutorial covered how to take user input, perform basic arithmetic operations, and use methods to organize your code. You can further enhance this application by adding more features, such as error handling, support for more operations, or even a graphical user interface.
Next Steps
Consider the following to expand your knowledge:
- Implement error handling to manage division by zero.
- Add functionality for more complex calculations, such as exponentiation or square roots.
- Explore how to build a graphical user interface using Windows Forms or WPF.