question

Singad-4815 avatar image
0 Votes"
Singad-4815 asked JackJJun-MSFT commented

How to make arithmetical operations in C#?

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 ;)

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You mean like this?

int a = 10;
int b = 5;

Console.WriteLine(a + b); // Addition
Console.WriteLine(a - b); // Subtraction
Console.WriteLine(a * b); // Multiplication
Console.WriteLine(a / b); // Division
0 Votes 0 ·

@Singad-4815, May I know if you have got any chance to check my answer? I am glad to help if you have any others questions.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@Singad-4815, 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.



image.png (16.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.