The use of Switch statement in c++

QuestionsThe use of Switch statement in c++
akankshat.ngPublished on: 8/7/2020 7:12:25 PM

Write a program in C to demonstrate the use of Switch statement. 


1 Answers
Answer is under review.

Switch case statements are a substitute for long if statements that compare a variable to several integral values

  • The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • Switch is a control statement that allows a value to change control of execution.

.............A program in C to demonstrate the use of Switch statement..................


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ch,x,y,z;
cout<<"1.ADDITION"<<endl;
cout<<"2.SUBTRACTION"<<endl;
cout<<"3.MULTIPLY"<<endl;
cout<<"4.DIVISION"<<endl;
cout<<"5.MODULUS"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch){
case 1:
cout<<"Enter first no:";
cin>>x;
cout<<"Enter second no:";
cin>>y;
z=x y;
cout<<"Addition of two no. is "<<z<<endl;
break;
case 2:
cout<<"Enter first no:";
cin>>x;
cout<<"Enter second no:";
cin>>y;
z=x-y;
cout<<"Subtraction of two no. is "<<z<<endl;
break;
case 3:
cout<<"Enter first no:";
cin>>x;
cout<<"Enter second no:";
cin>>y;
z=x*y;
cout<<"Multiplication of two no. is"<<z<<endl;
break;
case 4:
cout<<"Enter first no:";
cin>>x;
cout<<"Enter second no:";
cin>>y;
z=x/y;
cout<<"division of two no. is "<<z<<endl;
break;
case 5:
cout<<"Enter first no:";
cin>>x;
cout<<"Enter second no:";
cin>>y;
z=x%y;
cout<<"Modulus of two no. is "<<z<<endl;
break;
default:
cout<<"Wrong choice";
}
getch();
}


output: