Simple Calculator Console App Using C#

In this blog, you will learn how to create a simple calculator Console app using C# language.

This app will help end users to perform tasks, such as Addition, Subtraction, Multiplication, and Division.

To create the app, follow the steps shown below.

Create a new project

Open the Visual Studio editor. As you can see in the Start Page, there is an option for New Project. Click on “New Project”.

Create a Console Application

The following pop up appears. Select the Console Application. Create the project with name “CalculatorApp”, as shown below. The new project gets created successfully.

Calculator App Code Example

Here is the complete code of the app written in C#. In the program, there are four functional blocks–Addition, Subtraction, Multiplication, and Division. The user has to select one of the options for addition, subtraction, multiplication, and division respectively.

“Press 1 for Addition”.
“Press 2 for Subtraction”.
“Press 3 for Multiplication”.
“Press 4 for Division”.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace CalculatorApp {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             Console.WriteLine("Enter the action to be performed");  
  10.             Console.WriteLine("Press 1 for Addition");  
  11.             Console.WriteLine("Press 2 for Subtraction");  
  12.             Console.WriteLine("Press 3 for Multiplication");  
  13.             Console.WriteLine("Press 4 for Division \n");  
  14.             int action = Convert.ToInt32(Console.ReadLine());  
  15.             Console.WriteLine("Enter 1st input");  
  16.             int input_1 = Convert.ToInt32(Console.ReadLine());  
  17.             Console.WriteLine("Enter 2nd input");  
  18.             int input_2 = Convert.ToInt32(Console.ReadLine());  
  19.             int result = 0;  
  20.             switch (action) {  
  21.                 case 1: {  
  22.                     result = Addition(input_1, input_2);  
  23.                     break;  
  24.                 }  
  25.                 case 2: {  
  26.                     result = Subtraction(input_1, input_2);  
  27.                     break;  
  28.                 }  
  29.                 case 3: {  
  30.                     result = Multiplication(input_1, input_2);  
  31.                     break;  
  32.                 }  
  33.                 case 4: {  
  34.                     result = Division(input_1, input_2);  
  35.                     break;  
  36.                 }  
  37.                 default:  
  38.                     Console.WriteLine("Wrong action!! try again");  
  39.                     break;  
  40.             }  
  41.             Console.WriteLine("The result is {0}", result);  
  42.             Console.ReadKey();  
  43.         }  
  44.         //Addition  
  45.         public static int Addition(int input_1, int input_2) {  
  46.             int result = input_1 + input_2;  
  47.             return result;  
  48.         }  
  49.         //Substraction  
  50.         public static int Subtraction(int input_1, int input_2) {  
  51.             int result = input_1 + input_2;  
  52.             return result;  
  53.         }  
  54.         //Multiplication  
  55.         public static int Multiplication(int input_1, int input_2) {  
  56.             int result = input_1 + input_2;  
  57.             return result;  
  58.         }  
  59.         //Division  
  60.         public static int Division(int input_1, int input_2) {  
  61.             int result = input_1 + input_2;  
  62.             return result;  
  63.         }  
  64.     }  
  65. }  

Output of the Application

 
Simple Calculator Console App Using C#
 
In the above example, the user has selected the option “1” to perform addition. Then, the app asks the user to provide two inputs. After entering the inputs, the addition operation is performed and the user gets the desired output.

Likewise, the user has to choose the different options to perform different tasks.