Skip to main content
2 of 6
I corrected the variable mistake witch preveted the program from working and made some improvement to my code

My first code : A simple calculator

I'm a (really) early beginner in C#,

I'm reading this very good book "The C# Player's Guide" to learn C# and then plan to make video game with Unity 3D after.

In one of the chapter, he ask us to make a (really) simple calculator as a console application using the "switch statement" :

The program that we’ll make is going to be a simple calculator. We’re going to ask the user to type in two numbers and then type in a math operation to perform on the two numbers.

Use a switch statement to handle the different operations in different ways. Allow the user to type in ’+’ for addition, ’-’ for subtraction, ’*’ for multiplication, ’/’ for division, and ’%’ for remainder.

What do you think of my first program ? Is it well formatted and well written ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple calculator
{
  class Program
  {
    static void Main(string[] args)
    {
        //use for the string operation choice
        int operation = 0;
        //stock the result of the operation
        double result = 0;

        //Ask user first number
        Console.WriteLine("Type you first number :");
        string stringFirstNumber = Console.ReadLine();
        double firstNumber = Convert.ToDouble(stringFirstNumber);

        //Ask user second number
        Console.WriteLine("Type you second number :");
        string stringSecondNumber = Console.ReadLine();
        double secondNumber = Convert.ToDouble(stringSecondNumber);

        //Ask user operation to use
        Console.WriteLine("Enter the operation + (addition), - (soustraction), * (multiplication), / (division), ^ (exposant) or % (reste) :");
        string stringOperation = Console.ReadLine();

        // Convert string choice to integral
        if (stringOperation == "+" || stringOperation == "addition")
        {
            operation = 1;
        }
        else if (stringOperation == "-" || stringOperation == "soustraction")
        {
            operation = 2;
        }
        else if (stringOperation == "*" || stringOperation == "multiplication")
        {
            operation = 3;
        }
        else if (stringOperation == "/" || stringOperation == "division")
        {
            operation = 4;
        }
        else if (stringOperation == "^" || stringOperation == "exposant")
        {
            operation = 5;
        }
        else if (stringOperation == "%" || stringOperation == "reste")
        {
            operation = 6;
        }

        //Do someting depending on the operation choose
        switch (operation)
        {
            case 1:
                result = firstNumber + secondNumber;
                break;

            case 2:
                result = firstNumber - secondNumber;
                break;

            case 3:
                result = firstNumber * secondNumber;
                break;

            case 4:
                result = firstNumber / secondNumber;
                break;

            case 5:
                result = Math.Pow(firstNumber, secondNumber);
                break;

            case 6:
                result = firstNumber % secondNumber;
                break;
        }
        Console.WriteLine("\nResult of " + firstNumber + " " + stringOperation + " " + secondNumber + " = " + result + ".");
        Console.ReadKey();
    }
  }
}