Calculator in C#

Creating a Basic Calculator in C# Using Functions
Here's a C# program that implements a basic calculator using functions to handle different arithmetic operations:
using System;

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            double num1, num2, result;
            char op;

            Console.Write("Enter first number: ");
            num1 = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter second number: ");
            num2 = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter an operator (+, -, *, /): ");
            op = Convert.ToChar(Console.ReadLine());

            switch (op)
            {
                case '+':
                    result = Add(num1, num2);
                    break;
                case '-':
                    result = Subtract(num1, num2);
                    break;
                case '*':
                    result = Multiply(num1, num2);
                    break;
                case '/':
                    result = Divide(num1, num2);
                    break;
                default:
                    Console.WriteLine("Invalid operator");
                    return;
            }

            Console.WriteLine("Result: " + result);
        }

        static double Add(double a, double b)
        {
            return a + b;
        }

        static double Subtract(double a, double b)
        {
            return a - b;
        }

        static double Multiply(double a, double b)
        {
            return a * b;
        }

        static double Divide(double a, double b)
        {
            if (b == 0)
            {
                Console.WriteLine("Error: Division by zero");
                return 0;
            }
            return a / b;
        }
    }
}

Explanation:
 * Main Function:
   * Takes input for two numbers and an operator.
   * Calls the appropriate function based on the operator.
   * Prints the result.
 * Arithmetic Functions:
   * Add, Subtract, Multiply, and Divide functions perform the respective operations on the given numbers.
   * The Divide function includes a check for division by zero to handle potential errors.
Key Points:
 * Modular Design: Using functions makes the code more organized and reusable.
 * Error Handling: The Divide function demonstrates basic error handling for division by zero.
 * Input Validation: You can add more robust input validation to ensure users enter valid numbers and operators.
 * User Interface: For a more user-friendly experience, consider using a graphical user interface (GUI) or a command-line interface with clear prompts and error messages.
This basic calculator can be extended to include more advanced features like trigonometric functions, logarithmic functions, etc., by adding more functions to the program.

Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Kotlin Strings: Features and Operations Guide

Kotlin Android Program (QCR) Application Codes That Read Text in Photos