0% found this document useful (0 votes)
18 views7 pages

HEXCO

The document outlines a simple console-based calculator program that allows users to perform basic arithmetic operations including addition, subtraction, multiplication, and division. It features a menu for operation selection, input validation, and error handling for division by zero. The program continues to prompt the user until they choose to exit by selecting option 5.

Uploaded by

tinozzzacky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

HEXCO

The document outlines a simple console-based calculator program that allows users to perform basic arithmetic operations including addition, subtraction, multiplication, and division. It features a menu for operation selection, input validation, and error handling for division by zero. The program continues to prompt the user until they choose to exit by selecting option 5.

Uploaded by

tinozzzacky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Console.WriteLine("Welcome to Simple Calculator!

");

while (true)

DisplayMenu();

Console.Write("Select operation (1-5): ");

string choice = Console.ReadLine();

if (choice == "5")

Console.WriteLine("Thank you for using the calculator. Goodbye!");

break;
}

if (!IsValidChoice(choice))

Console.WriteLine("Invalid choice. Please select 1-5.");

continue;

double num1 = GetNumberInput("Enter first number: ");

double num2 = GetNumberInput("Enter second number: ");

PerformCalculation(choice, num1, num2);


Console.WriteLine("\nPress Enter to continue...");

Console.ReadLine();

static void DisplayMenu()

Console.WriteLine("\nCalculator Menu");

Console.WriteLine("1. Addition (+)");

Console.WriteLine("2. Subtraction (-)");

Console.WriteLine("3. Multiplication (×)");

Console.WriteLine("4. Division (÷)");


Console.WriteLine("5. Exit");

static bool IsValidChoice(string choice)

return choice == "1" || choice == "2" || choice == "3" || choice == "4" || choice == "5";

static double GetNumberInput(string prompt)

double number;

while (true)
{

Console.Write(prompt);

if (double.TryParse(Console.ReadLine(), out number))

return number;

Console.WriteLine("Invalid input. Please enter a number.");

static void PerformCalculation(string choice, double num1, double num2)

switch (choice)
{

case "1":

Console.WriteLine("\nResult: {num1} + {num2} = {num1 + num2}");

break;

case "2":

Console.WriteLine("\nResult: {num1} - {num2} = {num1 - num2}");

break;

case "3":

Console.WriteLine("\nResult: {num1} × {num2} = {num1 * num2}");

break;

case "4":

//- Clear prompts and formatted output


if (num2 == 0)

Console.WriteLine("Error: Cannot divide by zero!");

else

Console.WriteLine("\nResult: {num1} ÷ {num2} ="+num1/num2);

break;

You might also like