John's little brother is struggling with math. He decided to design a calculator with basic operations such as addition, subtraction, and multiplication.The calculator should receive two numbers and an operator from the user. It should operate according to the operator entered and display the result. Help him implement the calculator using the Switch statement.
Question
John's little brother is struggling with math. He decided to design a calculator with basic operations such as addition, subtraction, and multiplication.The calculator should receive two numbers and an operator from the user. It should operate according to the operator entered and display the result. Help him implement the calculator using the Switch statement.
Solution
Sure, here is a simple implementation of a calculator using the Switch statement in JavaScript:
function calculator(num1, num2, operator) {
let result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
default:
console.log('Invalid operator');
return;
}
console.log('The result is: ' + result);
}
// Test the function
calculator(5, 2, '+'); // Output: The result is: 7
calculator(5, 2, '-'); // Output: The result is: 3
calculator(5, 2, '*'); // Output: The result is: 10
In this code, we define a function calculator that takes three parameters: num1, num2, and operator. Inside the function, we use a switch statement to perform different operations based on the operator. If the operator is '+', we add num1 and num2. If it's '-', we subtract num2 from num1. If it's '*', we multiply num1 and num2. If the operator is not any of these, we print an error message and return from the function. After performing the operation, we print the result.
Similar Questions
Single File Programming QuestionProblem StatementJohn's little brother is struggling with math. He decided to design a calculator with basic operations such as addition, subtraction, and multiplication.The calculator should receive two numbers and an operator from the user. It should operate according to the operator entered and display the result. Help him implement the calculator using the Switch statement.Input format :The first line consists of two space-separated integers num1 and num2.The second line consists of a character, representing the operator: + for addition, - for subtraction, * for multiplicationOutput format :The output displays an integer representing the result after the operation is performed.If the operator is invalid, the output displays "INVALID".Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ num1, num2 ≤ 100Sample test cases :Input 1 :1 100 +Output 1 :101Input 2 :2 95 -Output 2 :-93Input 3 :34 56 *Output 3 :1904Input 4 :1 2 ^Output 4 :INVALID
Charlie, a novice programmer, is developing a simple calculator program to perform basic arithmetic operations. He needs your assistance in refining the program.Write a program that takes an operator ('+', '-', '*', or '/') and two operands as input. The program should then perform the corresponding operation and display the result. If the input is an invalid operator, the program should display an appropriate error message.Function Specifications:double add(double x, double y)double subtract(double x, double y)double multiply(double x, double y)double divide(double x, double y)Input format :The first line of input consists of a character c, representing the operator (+, -, *, /).The second line consists of two space-separated double values a and b, representing the operands.Output format :
.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.
Create a mini calculator which has the ability to add, subtract, multiply and divide. Follow the steps below to complete this assignment.Step 1: Your code should be able to add, subtract, divide and multiplyStep 2: Your program should ask the user for two numbers.Step 3: Your code should calculate and print the output for all the calculations. Use appropriate variables and output names.
Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.