Learning something new like a programming language comes with a project to solidify your understanding of the underlying concepts and fundamentals. To know something deeply, do not rush through the fundamentals as it leaves gaps in your knowledge.
This guide shows you how to build a console-based calculator in C# and interact with the interface in your terminal.
Prerequisites
As a first-timer to C#, I expect you to know how to declare variables and use the various data types. As you know, C# supports strong type checking and it is a simple, modern, general-purpose, object-oriented programming language.
Some of the important types include:
- bool
- int
- long
- float
- double
- char
For the console-based calculator, these are some of the features that will make a working app:
- Ability to ask the user for two numbers and a calculation method
- Valid calculation methods like addition, exponent, modulo, multiplication, division, and subtraction
- Calculate the result and print it to the console after input from the user
- After the result is printed, ask the user for another calculation. The program ends when the user decides by typing "no"
Let's begin.
Creating the Calculator App
In your Program.cs
file within the directory, copy-paste this code snippet replacing the Hello World script.
Console.WriteLine("Console bassed calculator\n");
Console.WriteLine("========================");
bool continueCalculating = true;
while (continueCalculating)
{
Console.WriteLine("Enter your first number: ");
string? firstInput = Console.ReadLine();
if (firstInput == null)
{
Console.WriteLine("First input cannot be null.");
return;
}
double number1 = double.Parse(firstInput);
Console.WriteLine("Enter the second number: ");
string? secondInput = Console.ReadLine();
if (secondInput == null)
{
Console.WriteLine("Second input cannot be null.");
return;
}
double number2 = double.Parse(secondInput);
Console.WriteLine("Choose an operation (multiply, divide, subtract, add, exponent, modulo) ");
string? operation = Console.ReadLine().ToLower();
switch (operation)
{
case "multiply":
Console.WriteLine($"Result: {Math.Round(number1 * number2, 2)}");
break;
case "divide":
if (number2 != 0)
Console.WriteLine($"Result: {Math.Round(number1 / number2, 2)}");
else
Console.WriteLine("Error: Division by zero is not allowed.");
break;
case "subtract":
Console.WriteLine($"Result: {Math.Round(number1 - number2, 2)}");
break;
case "add":
Console.WriteLine($"Result: {Math.Round(number1 + number2, 2)}");
break;
case "exponent":
Console.WriteLine($"Result: {Math.Round(Math.Pow(number1, number2), 2)}");
break;
case "modulo":
if (number2 != 0)
{
Console.WriteLine($"Result: {number1 % number2}");
}
else
{
Console.WriteLine("Error: Modulo by zero is not allowed.");
}
break;
default:
Console.WriteLine("Error: Unsupported operator. Please use 'multiply', 'divide', 'subtract', or 'add'.");
break;
}
Console.WriteLine("Would you like to perform another calculation? (yes to continue, no to exit): ");
string? response = Console.ReadLine()?.Trim().ToLower();
if (response != "yes")
{
Console.WriteLine("Thank you for using the calculator. Goodbye!");
break;
}
}
The block of code above ensures the result from this app gives the accurate result of any calculation from the user and it also makes use of the Switch statements.
Running the program
Use the command, dotnet run
in your terminal or command prompt.
You can find the complete source code in this GitHub Gist.
Share your thoughts on what you think about this project.
Top comments (0)