Â
In this, we are going to see the topic of Switch case in C++.
This is a program of Switch Case in which we are going to make a simple calculator to perform addition, subtraction, multiplication, division.
The code given below can be used for TURBO C++ Compiler:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout << "\n **** This Program is of Switch Case **** \n";
char operations;
cout << "Select the Operation you would like to Perform from the list below. \n";
cout << "1. For Addition press '+' \n";
cout << "2. For Subtraction press '-' \n";
cout << "3. For Multiplication press '*' \n";
cout << "4. For Division press '/' \n";
cin >> operations;
switch (operations)
{
case '+':
int a, b, result;
cout << " *** Addition ***\n";
cout << "Enter two number to perform addition\n";
cin >> a >> b;
result = a + b;
cout << "The Addition of two numbers is = " << result << endl;
break;
case '-':
cout << " *** Subtraction ***\n";
cout << "Enter two number to perform subtraction\n";
cin >> a >> b;
result = a - b;
cout << "The Subtraction of two numbers is = " << result << endl;
break;
case '*':
cout << " *** Multiplication ***\n";
cout << "Enter two number to perform multiplication\n";
cin >> a >> b;
result = a * b;
cout << "The Multiplication of two numbers is = " << result << endl;
break;
case '/':
cout << " *** Division ***\n";
cout << "Enter two number to perform division\n";
cin >> a >> b;
result = a / b;
cout << "The Division of two numbers is = " << result << endl;
break;
default:
cout << "Oops!! Your input is not accepted.\n";
cout << "Please read the instructions carefully\n";
break;
}
cout << "Thank You";
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include<iostream>
using namespace std;
int main()
{
cout << "\n **** This Program is of Switch Case **** \n";
char operations;
cout << "Select the Operation you would like to Perform from the list below. \n";
cout << "1. For Addition press '+' \n";
cout << "2. For Subtraction press '-' \n";
cout << "3. For Multiplication press '*' \n";
cout << "4. For Division press '/' \n";
cin >> operations;
switch (operations)
{
case '+':
int a, b, result;
cout << " *** Addition ***\n";
cout << "Enter two number to perform addition\n";
cin >> a >> b;
result = a + b;
cout << "The Addition of two numbers is = " << result << endl;
break;
case '-':
cout << " *** Subtraction ***\n";
cout << "Enter two number to perform subtraction\n";
cin >> a >> b;
result = a - b;
cout << "The Subtraction of two numbers is = " << result << endl;
break;
case '*':
cout << " *** Multiplication ***\n";
cout << "Enter two number to perform multiplication\n";
cin >> a >> b;
result = a * b;
cout << "The Multiplication of two numbers is = " << result << endl;
break;
case '/':
cout << " *** Division ***\n";
cout << "Enter two number to perform division\n";
cin >> a >> b;
result = a / b;
cout << "The Division of two numbers is = " << result << endl;
break;
default:
cout << "Oops!! Your input is not accepted.\n";
cout << "Please read the instructions carefully\n";
break;
}
cout << "Thank You";
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP