Complementary task for topic: 2
M Nemeth · 2023-08-29 15:21:04.612222'
Switch/case:calculator
Switch/case:calculator
Write a C program that acts as a simple calculator. The program should prompt the user to enter two numbers and an operator (+, -, *, /). Perform the corresponding arithmetic operation based on the entered operator and display the result.
Hint: The characters can be used as integers!
Solution
Explanation
In this example, the program prompts the user to enter two numbers and an operator (+, -, *, /). The first number is stored in the num1 variable, the second number is stored in the num2 variable, and the operator is stored in the operator variable using scanf(). The program uses a switch statement to perform the corresponding arithmetic operation based on the entered operator. When the operator matches one of the case labels, the corresponding arithmetic operation is performed and the result is stored in the result variable. After performing the arithmetic operation, the program uses printf() to display the result as specified in the task.