Amit wants to develop an arithmetic calculator to perform some arithmetic functions. Develop a program to achieve the same
using System;
namespace Calculator
{
internal class Program
{
static void Main(string[] args)
{
bool quit = false;
bool flag = false;
float first = 0;
float second = 0;
char operation = '\0';
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine();
Console.WriteLine(" + : Addition of number");
Console.WriteLine(" - : Number subtraction");
Console.WriteLine(" * : Multiplication of numbers");
Console.WriteLine(" / : Division of numbers");
Console.WriteLine(" q : Exit from the program ");
Console.ForegroundColor = ConsoleColor.Green;
do
{
try
{
Console.Write("\nEnter a first number: ");
first = float.Parse(Console.ReadLine());
Console.Write("Enter a second number: ");
second = float.Parse(Console.ReadLine());
Console.Write("specify the operation: ");
operation = char.Parse(Console.ReadLine());
}
catch
{
flag = true;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\aRead the instructions carefully!!!");
Console.ForegroundColor = ConsoleColor.Green;
}
if(!flag)
{
switch (operation)
{
case '+': Console.WriteLine("The sum of the numbers is: " + (first + second));break;
case '-': Console.WriteLine("The difference between the numbers is: " +(first - second));break;
case '*': Console.WriteLine("The product of the numbers is: " + (first * second));break;
case '/': Console.WriteLine("The ratio of the numbers is: " + (first / second));break;
case 'q': quit = true; break;
default:
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\aRead the instructions carefully!!!");
Console.ForegroundColor = ConsoleColor.Green;
}
break;
}
}
flag = false;
}
while(!quit);
}
}
}
Comments
Leave a comment