In this tutorial you will learn about the C++ Program to perform addition, subtraction, multiplication and division using Switch and its application with practical example.
C++ Program to Perform Addition, Subtraction, Multiplication, and Division using Switch
In this tutorial, we will learn to create a C++ program. After that will perform Addition, Subtraction, Multiplication, and Division in C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ Programming.
Program for performing the Addition, Subtraction, Multiplication, and Division:-
Every Programming language is having many predefined functions for arithmetic operations. In the C++ programming language, we will do the Addition, Subtraction, Multiplication, and Division operations with the help of Arithmetic operators using the switch case.
Algorithm for procedure:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
1. Declaring the variables for the program. 2. Taking input operator for operations. 2. Taking the input numbers from the user in number format. 3. Adding the numbers and storing it in a variable. 4. Subtracting the numbers and storing it in a variable. 5. Multiplying the numbers and storing it in a variable. 6. Dividing the numbers and storing it in a variable. 7. Printing the Outputs. 8. End program. |
Program for Addition, Subtraction, Multiplication and Division:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* C++ Program to Perform Addition, Subtraction, Multiplication, and Division of Two Numbers using switch case*/ # include <iostream> using namespace std; int main() { //declaring the requred variables for the program. char op; float no1, no2; //taking input symbol from the user for performing the calculations cout << "Enter operator: +, -, *, /: "; cin >> op; //Taking the two numbers for calculations cout << "Enter two operands/ numbers for operations: "; cin >> no1 >> no2; switch(op) { case '+': //Priniting the add operation for the values. cout << no1 << " + " << no2 << " = " << no1 + no2; break; case '-': //Priniting the subtract operation for the values. cout << no1 << " - " << no2 << " = " << no1 - no2; break; case '*': //Priniting the multiply operation for the values. cout << no1 << " * " << no2 << " = " << no1 * no2; break; case '/': //Priniting the divide operation for the values. cout << no1 << " / " << no2 << " = " << no1 / no2; break; default: // If the operator is other than +, -, * or /, error message is shown cout << "Error! operator is not correct"; break; } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no1= it will hold the input number value from the user.
- no2 = it will hold the input number value from the user.
- op = it will hold the character value for the operator.
Taking Input operator from the user.
Taking input number from the user.
Program code for operations.