Challenge 4: Implement a Calculator Class
In this exercise, you have to implement a calculator which can perform addition, subtraction, multiplication, and division.
We'll cover the following
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
andnum2
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 1400+ tech skills courses.