Related Video
Code:
//Calculator using switch statement
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float n1,n2,res;
int ch;
cout<<"\n\t================================================================\n"<<endl;
cout<<"\n\t Calculator using Switch "<<endl;
cout<<"\n\t================================================================\n"<<endl;
do
{
cout<<"Enter Two Numbers \n\n";
cout<<"Number 1 : ";cin>>n1;
cout<<"Number 2 : ";cin>>n2;
cout<<"\nEnter choice what you want to calculate\n\n";
cout<<"1-Addition(+)\n";
cout<<"2-Subtract(-)\n";
cout<<"3-Multiply(*)\n";
cout<<"4-Divide(/)\n";
cout<<"5-Modulus(%)\n";
cout<<"6-Exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<n1<<" + "<<n2<<" = "<<n1+n2<<endl;
break;
case 2:
cout<<n1<<" - "<<n2<<" = "<<n1-n2<<endl;
break;
case 3:
cout<<n1<<" * "<<n2<<" = "<<n1*n2<<endl;
break;
case 4:
cout<<n1<<" / "<<n2<<" = "<<n1/n2<<endl;
break;
case 5:
cout<<(int)n1<<" % "<<(int)n2<<" = "<<(int)n1%(int)n2<<endl;
break;
default:
cout<<"Thank you for using this :) \n";
getch();
}
}
while(ch>0 && ch<6);
}
Output:
================================================================
Calculator using Switch
================================================================
Enter Two Numbers
Number 1 : 6
Number 2 : 3
Enter choice what you want to calculate
1-Addition(+)
2-Subtract(-)
3-Multiply(*)
4-Divide(/)
5-Modulus(%)
6-Exit
3
6 * 3 = 18
Enter Two Numbers
Number 1 : 4
Number 2 : 1
Enter choice what you want to calculate
1-Addition(+)
2-Subtract(-)
3-Multiply(*)
4-Divide(/)
5-Modulus(%)
6-Exit
2
4 - 1 = 3
Enter Two Numbers
Number 1 : 5
Number 2 : 9
Enter choice what you want to calculate
1-Addition(+)
2-Subtract(-)
3-Multiply(*)
4-Divide(/)
5-Modulus(%)
6-Exit
6
Thank you for using this :)