Answer to Question #297265 in C# for Akane.

Question #297265

Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the number, the operator and the result. (For division, if the denominator is zero, output an appropriate message). CODING C# ​

1
Expert's answer
2022-02-13T09:42:29-0500
using System;
using System.Collections.Generic;
using System.Globalization;


namespace App
{
    class Program
    {


        static void Main(string[] args)
        {




            char operation;
            Console.Write("Enter the first number: ");
            int number1 = int.Parse(Console.ReadLine());
            Console.Write("Enter the second number: ");
            int number2 = int.Parse(Console.ReadLine());
            Console.Write("Enter the mathematical operation (+, -, *, /): ");
            operation = Console.ReadLine()[0];
            switch (operation)
            {
                case '+':
                    {
                        int result = number1 + number2;
                        Console.WriteLine("{0} + {1} = {2}", number1, number2, result);
                        break;
                    }
                case '-':
                    {
                        int result = number1 - number2;
                        Console.WriteLine("{0} - {1} = {2}", number1, number2, result);
                        break;
                    }
                case '*':
                    {
                        int result = number1 * number2;
                        Console.WriteLine("{0} * {1} = {2}", number1, number2, result);
                        break;
                    }
                case '/':
                    {


                        if (number2 != 0)
                        {
                            double result = (double)number1 / (double)number2;
                            Console.WriteLine("{0} / {1} = {2}", number1, number2, result);
                        }
                        else
                        {
                            Console.WriteLine("ERROR: the denominator is zero.");
                        }


                        break;
                    }
                default:
                    {
                        Console.WriteLine("Wrong operation.");
                        break;
                    }
            }




            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS