Calculator Project
In this classic starter project of using C#, I created a calculator. This was thought out in three simple steps:
1: Getting the Input
2: Storing Values
3: Doing the Calculations
Getting the input was simple enough. Just type in the numbers using the keyboard.
Storing the values was more difficult. The numbers had to be stored as variables in the program.
Making sure the calculations came out after the user hits enter or equals.
C#
using System;
class Program
{
static void Main()
{
Console.Write(“Enter first number: “)
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write(“Enter operator (+,-,*,/): “);
string op = Console.Readline();
Console.Write(“Enter second number: “);
double num2 = Convert.ToDouble(Console.Readline());
double result = 0;
if (op == “+”)
result = num1 + num2;
else if (op == “-“)
result = num1 – num2;
else if (op == “*”)
result = num1 * num2;
else if (op == “/”)
result = num2 != 0 ? num1 / num2 : 0;
Console.WriteLine(“Result: ” + result);
}
}