If you have started coding in C#, then you might want to understand how to perform basic Arithmatic operations in C# using console application, so in this article, I have mentioned how to create a calculator in c# console application.
So We will declare 2 variable to store numbers, while third variable to store results of calculator operation and 4th variable to store choice.
Using 4th variable (choice), we will check what Arithmatic Operation we need to apply using Switch case in C#.
Here is the complete C# program
using System;
namespace CalculatorCsharp
{
public class Program
{
static void Main(string[] args)
{
int firstNumber, secondNumber;
float result;
int choice;
string value;
do
{
//Calculator Program in Console
Console.WriteLine("======== CALCULATOR PROGRAM FOR BEGINNER in C# ==========");
Console.WriteLine("1. Addition OPERATION(+).");
Console.WriteLine("2. Subtract OPERATION(-).");
Console.WriteLine("3. Multiply OPERATION(*).");
Console.WriteLine("4. Divide OPERATION(/).");
Console.WriteLine("\nENTER YOUR FIRST NUMBER:- ");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("ENTER YOUR SECOND NUMBER:- ");
secondNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nENTER YOUR CHOICE ( 1 - 4):- ");
//get user input
choice = Convert.ToInt32(Console.ReadLine());
//using switch case
//once choice is entered by user
switch (choice)
{
//do addition if 1 is selected
case 1:
result = firstNumber + secondNumber;
Console.WriteLine("Result Is :- " + result);
break;
//do subtraction if 2 is selected
case 2:
result = firstNumber - secondNumber;
Console.WriteLine("Result Is :- " + result);
break;
case 3:
result = firstNumber * secondNumber;
Console.WriteLine("Result Is :- " + result);
break;
case 4:
result = firstNumber / secondNumber;
Console.WriteLine("Result Is :- " + result);
break;
}
Console.Write("Do you want to continue(y/n):");
value = Console.ReadLine();
}
while (value == "y" || value == "Y");
}
}
}
In the above code, we are also using do-while loop of C#, basically we will continue asking user if they need to do more calculations work, then click press "Y" otherwise "N"
If "Y" is entered, we ask user to enter next 2 numbers for Arithmatic operation.
Here is the output
======== CALCULATOR PROGRAM FOR BEGINNER in C# ==========
1. Addition OPERATION(+).
2. Subtract OPERATION(-).
3. Multiply OPERATION(*).
4. Divide OPERATION(/).
ENTER YOUR FIRST NUMBER:-
44
ENTER YOUR SECOND NUMBER:-
55
ENTER YOUR CHOICE ( 1 - 4):-
2
Result Is :- -11
Do you want to continue(y/n):y
======== CALCULATOR PROGRAM FOR BEGINNER in C# ==========
1. Addition OPERATION(+).
2. Subtract OPERATION(-).
3. Multiply OPERATION(*).
4. Divide OPERATION(/).
ENTER YOUR FIRST NUMBER:-
112
ENTER YOUR SECOND NUMBER:-
55
ENTER YOUR CHOICE ( 1 - 4):-
3
Result Is :- 6160
Do you want to continue(y/n):n
That's it.
You may also like to read:
How to add Double quotes in String in C#?
Extract Text from image in C# using Tesseract
Multiple Fields Order By With Linq
Sort Dictionary by Value in C#