C SHARPE

PART-A

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6

PART-B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 PROGRAM B8 . . .

 
  
  
 /*C# program to call math operations (Any 4) using delegates. */
 using System;

// Define a delegate for math operations
delegate double MathOperation(double x, double y);

class MathOperations
{
    // Methods representing different math operations
    static double Add(double x, double y)
    {
        return x + y;
    }

    static double Subtract(double x, double y)
    {
        return x - y;
    }

    static double Multiply(double x, double y)
    {
        return x * y;
    }

    static double Divide(double x, double y)
    {
        if (y != 0)
        {
            return x / y;
        }
        else
        {
            Console.WriteLine("Cannot divide by zero.");
            return double.NaN; // Not a number
        }
    }

    static void Main()
    {
        // Create instances of the delegate for each math operation
        MathOperation addDelegate = Add;
        MathOperation subtractDelegate = Subtract;
        MathOperation multiplyDelegate = Multiply;
        MathOperation divideDelegate = Divide;

        // Perform math operations using delegates
        double resultAdd = PerformOperation(addDelegate, 10, 5);
        double resultSubtract = PerformOperation(subtractDelegate, 10, 5);
        double resultMultiply = PerformOperation(multiplyDelegate, 10, 5);
        double resultDivide = PerformOperation(divideDelegate, 10, 5);

        // Display results
        Console.WriteLine($"Addition Result: {resultAdd}");
        Console.WriteLine($"Subtraction Result: {resultSubtract}");
        Console.WriteLine($"Multiplication Result: {resultMultiply}");
        Console.WriteLine($"Division Result: {resultDivide}");
    }

    // Helper method to perform a math operation using a delegate
    static double PerformOperation(MathOperation operation, double x, double y)
    {
        return operation(x, y);
    }
}