- Open Visual Studio or any C# IDE of your choice.
- Copy and paste the code from the provided source file into the IDE.
- Compile and run the program.
- Follow the on-screen instructions to perform calculations.
- The program performs basic arithmetic operations: addition, subtraction, multiplication, and division.
- It validates the user input to ensure that only numerical values are accepted.
- It handles division by zero gracefully, displaying an appropriate error message.
This program functions as a basic calculator, allowing users to input two numbers and select an operation (addition, subtraction, multiplication, or division). The program then computes and displays the result of the chosen operation.
- Input validation: Ensures both inputs are valid numbers.
- Operation selection: Supports the arithmetic operators (
+
,-
,*
,/
). - Error handling: Detects invalid inputs and handles division by zero.
- The user is prompted to input the first number.
- If the input is invalid, the program displays an error message and terminates.
- The user is prompted to input the second number.
- Similarly, invalid input leads to an error message and termination.
- The user is asked to select an arithmetic operation by entering one of the symbols (
+
,-
,*
,/
). - Depending on the operation:
- Addition (
+
): Adds the two numbers and displays the result. - Subtraction (
-
): Subtracts the second number from the first and displays the result. - Multiplication (
*
): Multiplies the two numbers and displays the result. - Division (
/
): Divides the first number by the second and displays the result if the second number is not zero. If it is zero, an error message is displayed.
- Addition (
- For invalid operation symbols, the program outputs "Invalid operation."
-
Input Handling:
- Both numbers are read using
Console.ReadLine()
and validated withdouble.TryParse
. - The operation symbol is read and processed using a
switch
statement.
- Both numbers are read using
-
Operation Execution:
- The chosen arithmetic operation is executed based on the symbol provided by the user.
-
Output Messages:
- Clear and user-friendly messages display results or errors (e.g., invalid input, division by zero).
This calculator program is a simple yet powerful tool for performing basic arithmetic. It prioritizes user input validation and includes error handling to ensure smooth execution. The design is ideal for educational purposes and for users learning basic C# programming concepts.