1.
Write a function called
calculator
which takes two doubles
, first
and second
and a char operation
as parameters. calculator
performs addition, subtraction, multiplication, or division with the two doubles
depending on what operation is passed in (+, -, \*, /). It then returns the result. Run and test your code!
Solution.
Below is one way to implement the
calculator
function. Using conditionals, we return the correct result depending on which operation was given.
double calculator(double first, double second, char operation) {
if (operation == '+') {
return first + second;
}
else if (operation == '-') {
return first - second;
}
else if (operation == '*') {
return first * second;
}
else {
return first / second;
}
}