How to make arithmetical operations in C#?

Singad 1 Reputation point
2021-10-19T17:09:18.02+00:00

I'm making a calculator, and I don't know what signs needs to be for Addition, Subtraction and Division.

  if (aritmeticalOperation == "Multiplication") //It can be other operation too.
                        {
                            long num01;
                            Int64 num02;


                            Console.Write("\n\nType a number to be multiplied: ");
                            num01 = Convert.ToInt64(Console.ReadLine());
                            Console.Write("Type another number: ");
                            num02 = Convert.ToInt64(Console.ReadLine());
                            Console.WriteLine("\nThe result is " + num01 * num02); //multiplication works but I don't know what should I put for other operations.
                            while (Console.ReadKey().Key != ConsoleKey.Enter) ;
                        }

Thanks for help ;)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,281 Reputation points Microsoft Vendor
    2021-10-20T02:02:25.713+00:00

    @Singad , based on my research, you could try the following code to make make arithmetical operations in C#.

     while (true)  
                {  
                    Console.WriteLine("Please input x");  
                    double x = Convert.ToDouble(Console.ReadLine());  
                    Console.WriteLine("Please input y");  
                    double y = Convert.ToDouble(Console.ReadLine());  
                    Console.WriteLine("// Please input opeartions //");  
                     
                    string z = Console.ReadLine();  
                    switch (z)  
                    {  
                        case "+":  
                            Console.WriteLine("// Result is //");  
                            Console.WriteLine(x+y);  
                            break;  
                        case "-":  
                            Console.WriteLine("// Result is //");  
                            Console.WriteLine(x-y);  
                            break;  
                        case "*":  
                            Console.WriteLine("// Result is //");   
                            Console.WriteLine(x*y);  
                            break;  
                        case "/":  
                            if (y == 0)  
                            {  
                                Console.WriteLine("Result is 0");  
                            }  
                            else  
                            {  
                                Console.WriteLine("// Result is //");  
                                Console.WriteLine(x/y);  
                            }  
                            break;  
                    }  
                    Console.WriteLine("Do you want to exit ?! Y:N?");  
                    char Terminat = Console.ReadKey().KeyChar;  
                    if (Terminat == 'Y' || Terminat == 'y')  
                    {  
                        break;  
                    }  
      
                }  
    

    Result:

    141839-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments