Create the equivalent of a four-function calculator. The program should request the user to enter a number, an operator, and another number (Use floating-point numbers). It should then carry out the specific arithmetic operation: addition, subtraction, multiplication or division of two numbers. As you know, division by zero is not defined. So program must display an error message if division by zero occurs. You may use a switch statement to select the operation. Finally display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The answer can be ‘y’ or ‘n’ (may use uppercase y or n also).
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
IN PYTHON
Create the equivalent of a four-function calculator. The program should request the user to enter a number, an operator, and another number (Use floating-point numbers). It should then carry out the specific arithmetic operation: addition, subtraction, multiplication or division of two numbers. As you know, division by zero is not defined. So program must display an error message if division by zero occurs. You may use a switch statement to select the operation. Finally display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The answer can be ‘y’ or ‘n’ (may use uppercase y or n also). The following output shows the results of two operations.
Enter first number: 15
Enter an operator: /
Enter second number: 2
15/2 = 7.50
Do another (Enter y or Y for yes or n or N for no)? y
Enter first number: 12
Enter an operator: +
Enter second number: 125
12 + 125 = 137
Do another (Enter y or Y for yes or n or N for no)?
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images