Simple Calculator Program in C++ Language:
In this program, we are using only four basic mathematical operations like addition, subtraction, multiplication, and division. We perform these operations with the help of switch statement in C++.
Program Code:
#include<iostream>
using namespace std;
int main()
{
int a, b, res;
char choice;
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Multiplication\n";
cout<<"4. Division\n";
cout<<"5. Exit\n";
cout<<"Enter your choice for Arithmetic operation : ";
cin>>ch;
cout<<endl;
switch(ch)
{
case '1' : cout<<"Enter two numbers a and b : ";
cin>>a>>b;
cout<<endl;
res=a+b;
cout<<"Result = "<<res;
break;
case '2' : cout<<"Enter two numbers a and b : ";
cin>>a>>b;
cout<<endl;
res=a-b;
cout<<"Result = "<<res;
break;
case '3' : cout<<"Enter two numbers a and b : ";
cin>>a>>b;
cout<<endl;
res=a*b;
cout<<"Result = "<<res;
break;
case '4' : cout<<"Enter two numbers a and b : ";
cin>>a>>b;
cout<<endl;
res=a/b;
cout<<"Result = "<<res;
break;
case '5' : exit(0);
break;
default : cout<<"Wrong Choice Entered........!!";
break;
}
cout<<"\n---------------------------------------------\n";
return 0;
}
The program output is tested on www.codechef.com
Output:
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your choice for Arithmetic operation : 1
Enter two numbers a and b : 2 3
Result = 5
---------------------------------------------------
Thanks
Mukesh Rajput
Post A Comment:
0 comments: