Challenge 4: Implement a Calculator Class

In this exercise, you have to implement a calculator which can perform addition, subtraction, multiplication, and division.

Problem Statement

Write a C# class called Calculator with:

  • private fields:

    • _num1 and _num2 (double type)
  • Methods:

    • Add(), a method which returns the sum of two numbers.

    • Subtract(), a method which returns the difference of _num1 and _num2 (num2 - num1).

    • Multiply(), a method which returns the result of multiplication of numbers.

    • Divide(), a method which returns the result of division of _num2 by _num1.

  • Define a parameterized constructor which takes two parameters num1 and num2 and assigns these parameters to the respective fields in the class.

Input

Passing numbers in the parameterized constructor

Output

Addition, Subtraction, Division, and Multiplication

Sample Input

Calculator calc = new Calculator(10, 94);
calc.Add()
calc.Subtract()
calc.Multiply()
calc.Divide()

Sample Output

104
84
940
9.4

Get hands-on with 1200+ tech skills courses.