Simple program #9 (Source code to calculator
program)
Enter first number: 5
Enter second number: 6
Choose an operator type from +, -, *,
/, %: *
O/p: -
5 * 6 = 30
The program is written in C#
programming language and will accept two numbers as input. The logic of the
program is to do calculation based on the input operator.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.Simple_program
{
class Simple9 //Calculator program
{
static void Main(string[] args)
{
try
{
Console.Write("Enter first number: ");
int firstNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int secondNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Choose an operator type from
+, -, *, /, %: ");
char operatorSymbol = Convert.ToChar(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("-----Output-----");
Console.WriteLine();
switch (operatorSymbol)
{
case '+':
Console.WriteLine("{0} + {1} = {2}", firstNumber,
secondNumber, firstNumber + secondNumber);
break;
case '-':
Console.WriteLine("{0} - {1} = {2}", firstNumber,
secondNumber, firstNumber - secondNumber);
break;
case '*':
Console.WriteLine("{0} * {1} = {2}", firstNumber,
secondNumber, firstNumber * secondNumber);
break;
case '/':
Console.WriteLine("{0} / {1} = {2}", firstNumber,
secondNumber, (double)(firstNumber
/ secondNumber));
break;
case '%':
Console.WriteLine("{0} % {1} = {2}", firstNumber,
secondNumber, firstNumber % secondNumber);
break;
default:
Console.WriteLine("Invalid ! operation");
break;
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Error! : " + ex.Message);
}
}
}
}
Output:
The input
numbers are 5 and 6. The program will do calculation based on the input
operator. The output is shown below:
Enter first number: 5
Enter second number: 6
Choose an operator type from +, -, *, /, %: *
5 * 6 = 30
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table Simple program
Keep learning and sharing...
No comments:
Post a Comment