Switch case statement is used when we have multiple conditions and we need to perform different actions based on the condition.
Below is an example in c++.
#include <iostream>
using namespace std;
int main(){
int firstnumber,secondnumber,result,option;
cout<<"-------Calculation of two number---------\n";
cout<<"Enter First Number: ";
cin>>firstnumber;
cout<<"Enter Second Number: ";
cin>>secondnumber;
cout<<"\nSelect a below option to perform the actions\n";
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Multiply\n";
cout<<"4. Division\n";
cout<<"Enter option: "
cin>>option;
switch(option) {
case 1:
result=firstnumber+secondnumber;
cout<<"Result of Addition is "<<result;
case 2:
result=firstnumber-secondnumber;
cout<<"Result of Subtraction is "<<result;
case 3:
result=firstnumber*secondnumber;
cout<<"Result of Multiply is "<<result;
case 4:
result=firstnumber/secondnumber;
cout<<"Result of Division is "<<result;
default:
cout<<"You have select a wrong option";
}
cout<<"\n\n~~~~~~~~~~~THANK YOU FOR VISITING HERE~~~~~~~~~~\n\n";
return 0;
}
Output
-------Calculation of two number---------
Enter First Number: 10
Enter Second Number: 20
Select a below option to perform the actions
1. Addition
2. Subtraction
Below is an example in c++.
#include <iostream>
using namespace std;
int main(){
int firstnumber,secondnumber,result,option;
cout<<"-------Calculation of two number---------\n";
cout<<"Enter First Number: ";
cin>>firstnumber;
cout<<"Enter Second Number: ";
cin>>secondnumber;
cout<<"\nSelect a below option to perform the actions\n";
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Multiply\n";
cout<<"4. Division\n";
cout<<"Enter option: "
cin>>option;
switch(option) {
case 1:
result=firstnumber+secondnumber;
cout<<"Result of Addition is "<<result;
case 2:
result=firstnumber-secondnumber;
cout<<"Result of Subtraction is "<<result;
case 3:
result=firstnumber*secondnumber;
cout<<"Result of Multiply is "<<result;
case 4:
result=firstnumber/secondnumber;
cout<<"Result of Division is "<<result;
default:
cout<<"You have select a wrong option";
}
cout<<"\n\n~~~~~~~~~~~THANK YOU FOR VISITING HERE~~~~~~~~~~\n\n";
return 0;
}
Output
-------Calculation of two number---------
Enter First Number: 10
Enter Second Number: 20
Select a below option to perform the actions
1. Addition
2. Subtraction
3. Multiply
4. Division
Enter Option: 3
Result of Multiply is 200
*************FOLLOW MY BLOG TO GET UPDATE FROM TECHNICAL GURU*********
4. Division
Enter Option: 3
Result of Multiply is 200
*************FOLLOW MY BLOG TO GET UPDATE FROM TECHNICAL GURU*********
No comments:
Post a Comment