Â
In this, we are going to see the topic of Switch case in C++.
This is program of Switch Case in which we are going to calculate Square and Cube of a number as per user's choice.
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";
float number;
char user_input;
cout<<"Enter the number to calculate the square or cube\n";
cin>>number;
cout<<"Select the operation from the list below\n";
cout<<"Press 's' for Square of the number\n";
cout<<"Press 'c' for Cube of the number\n";
cin>>user_input;
switch (user_input)
{
case 's':
float result;
result = number*number;
cout<<"The Square of "<<number<<" is = "<<result<<endl;
break;
case 'c':
result = number*number*number;
cout<<"The Cube of "<<number<<" 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";
float number;
char user_input;
cout<<"Enter the number to calculate the square or cube\n";
cin>>number;
cout<<"Select the operation from the list below\n";
cout<<"Press 's' for Square of the number\n";
cout<<"Press 'c' for Cube of the number\n";
cin>>user_input;
switch (user_input)
{
case 's':
float result;
result = number*number;
cout<<"The Square of "<<number<<" is = "<<result<<endl;
break;
case 'c':
result = number*number*number;
cout<<"The Cube of "<<number<<" 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