Hello World

Hpm7...CZcy
9 Jan 2024
24

using System;

// This C# console application simulates a basic calculator.
class CalculatorProgram
{
static void Main()
{
Console.WriteLine("Simple Calculator");

// Show the user operation options
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");

// Prompt the user to choose an operation
Console.Write("Please choose an operation (1-4): ");
int choice = Convert.ToInt32(Console.ReadLine());

// Get input numbers from the user
Console.Write("Enter the first number: ");
double number1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the second number: ");
double number2 = Convert.ToDouble(Console.ReadLine());

// Perform the operation and display the result
double result = 0;

switch (choice)
{
case 1:
result = number1 + number2;
break;
case 2:
result = number1 - number2;
break;
case 3:
result = number1 * number2;
break;
case 4:
if (number2 != 0)
result = number1 / number2;
else
Console.WriteLine("You cannot divide by zero.");
break;
default:
Console.WriteLine("Invalid choice. Please enter a number between 1 and 4.");
return;
}

Console.WriteLine($"Result: {result}");

// Wait for the user to press any key before exiting the application
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}

Get fast shipping, movies & more with Amazon Prime

Start free trial

Enjoy this blog? Subscribe to ugurtopal

0 Comments