Change the IF...ELSE statement below to SWITCH CASE the statement.
#include<iostream.h>
main()
int selection,quantity;
float price
cout<<"\Pen=Rm0.50";
cout<<"\n 2.Pencil=Rm0.30";
cout<<"\n 3.Ruler=Rm0.20";
cout<<"\ 4.eraser=Rm0.10";
cout<<\n\n Enter your selection= ";
cin>>selection;
cout<<"\n enter your quantity=";
cin>>quantity;
JAWAPAN IF..ELSE
#include<iostream.h>
main()
{
//declare variable;
int quantity;
cout<<"Enter your quantity:";
//if.....else
if(selection==1)
cout<<"price=RM"<<price;)
else if(selection==2)
{price=quantity*0.30;
cout<<"price=Rm"<<price;}
else if(selection==3)
{price=quantity*0.20;
cout<<"price=Rm"<<price;}
else if(selection==4)
{price=quantity*0.10;
cout<<"price=Rm"<<price;}
else
{cout<<"Invalid selection";}
return 0;
}
#include<iostream.h>
main()
int selection,quantity;
float price
cout<<"\Pen=Rm0.50";
cout<<"\n 2.Pencil=Rm0.30";
cout<<"\n 3.Ruler=Rm0.20";
cout<<"\ 4.eraser=Rm0.10";
cout<<\n\n Enter your selection= ";
cin>>selection;
cout<<"\n enter your quantity=";
cin>>quantity;
JAWAPAN IF..ELSE
#include<iostream.h>
main()
{
//declare variable;
int quantity;
cout<<"Enter your quantity:";
//if.....else
if(selection==1)
cout<<"price=RM"<<price;)
else if(selection==2)
{price=quantity*0.30;
cout<<"price=Rm"<<price;}
else if(selection==3)
{price=quantity*0.20;
cout<<"price=Rm"<<price;}
else if(selection==4)
{price=quantity*0.10;
cout<<"price=Rm"<<price;}
else
{cout<<"Invalid selection";}
return 0;
}
JAWAPAN SWITCH..CASE
#include<iostream.h>
main()
{
//declare variable;
int quantity;
cout<<Entter your quantity:";
//if.....else
if(selection==1)
cout<<"price=RM"<<price;)
else if(selection==2)
{price=quantity*0.30;
cout<<"price=Rm"<<price;}
else if(selection==3)
{price=quantity*0.20;
cout<<"price=Rm"<<price;}
else if(selection==4)
{price=quantity*0.10;
cout<<"price=Rm"<<price;}
else
{cout<<"Invalid selection";}
return 0;
}