Here's an example of a simple console application in C# that performs a calculation:
csharpusing System;
namespace CalculatorApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the operation (+, -, *, /):");
char operation = Convert.ReadKey().KeyChar;
Console.WriteLine();
double result = 0;
switch (operation)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
Console.WriteLine("Invalid operation");
break;
}
Console.WriteLine("Result: " + result);
Console.ReadKey();
}
}
}
In this example, the Program
class contains the Main
method which is the entry point for the application. The program first prompts the user to enter two numbers and an operation. The operation is then performed using a switch statement and the result is displayed on the console. The Convert.ToDouble
method is used to convert the string input from the user into a double type for calculation. The Convert.ReadKey().KeyChar
method is used to read the single character operation from the user. Finally, the result is displayed on the console and the program waits for the user to press any key before ending.