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#
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();
}
}
}
Comments
Leave a comment