write a c++ program to implement a simple calculator
Answers
Here is the program to make calc. in c++.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num1, num2;
char opr;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << endl;
cout << "Enter operator: + (addition), - (subtraction)," << " * (multiplication), /
(division): ";
cin >> opr;
cout << endl;
cout << num1 << " " << opr << " " << num2 << " = ";
switch (opr){
case '+':
cout << num1 + num2 << endl;
break;
case'-':
cout << num1 - num2 << endl;
break;
case'*':
cout << num1 * num2 << endl;
break;
case'/':
if (num2 != 0)
cout << num1 / num2 << endl;
else
cout << "ERROR \nCannot divide by zero" << endl;
break;
default:
cout << "Illegal operation" << endl;
}
return 0;
}
Hope It helps You!
Answer:
Explanation:
Here we use the concept of If else statement to run the code and get the output.
code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a,b,sum,diff,pro,div,mod;
char ch;
cout<<"Enter the first number"<<endl;
cin>>a;
cout<<"Enter the Second number"<<endl;
cin>>b;
cout<<"Enter the operation you need (+,-,*,/,%)"<<endl;
cin>>ch;
if(ch=='+')
{
sum=a+b;
cout<<"The sum of the entered two numbers is"<<sum<<endl;
}
else
if(ch=='-')
{
diff=a-b;
cout<<"The differnce of the entered two numbers is"<<diff<<endl;
}
else
if(ch=='*')
{
pro=a*b;
cout<<"The product of the entered two numbers is"<<pro<<endl;
}
else
if(ch=='/')
{
div=a/b;
cout<<"The quotient of the entered two numbers is"<<div<<endl;
}
else
if(ch=='%')
{
sum=a%b;
cout<<"The reminder of the entered two numbers is"<<mod<<endl;
}
else
cout<<"Invalid operator choosen"<<endl;
return 0;
}
Thanks!