Write a Program to Make a Simple Calculator in C++ Programming Language | Solved Programming Exercises
In this tutorial we will write a program in C++ Language to build a simple calculator. We will take two numbers from user and an operator. if operator is ‘+’ then add both numbers and so on.
We will use Dev C++ Compiler as an IDE.
How to install Dev C++ Compiler? Watch the Following Video.
So let’s Code in C++ Programming Language.
First of All, Create a C++ File then Include following Header Files:
#include"iostream"
using namespace std;
Above Header File is Used for Input and Output Purposes.
Second Step, take input from user.
main()
{
int a,b;
char op;
cout<<"Enter First Number = ";
cin>>a;
cout<<"Enter Operator(+,-,*,/) = ";
cin>>op;
cout<<"Enter Second Number = ";
cin>>b;
}
Next Step apply if condition to checker whether an operator is +,-,*/.
if(op == '+')
{
cout<<"Sum = "<<a+b;
}
else if(op == '-')
{
cout<<"Minus = "<<a-b;
}
else if(op == '*')
{
cout<<"Mul = "<<a*b;
}
else if(op == '/')
{
if(b == 0)
{
cout<<"Cannot divide by zero";
}
else
{
cout<<"Div"<<a/b;
}
}
else
{
cout<<"Invalid Operator";
}
Finally Complete Code is Here:
//write a program to build
a simple calculator in C++
#include"iostream"
using namespace std;
main()
{
int a,b;
char op;
cout<<"Enter First Number = ";
cin>>a;
cout<<"Enter Operator(+,-,*,/) = ";
cin>>op;
cout<<"Enter Second Number = ";
cin>>b;
if(op == '+')
{
cout<<"Sum = "<<a+b;
}
else if(op == '-')
{
cout<<"Minus = "<<a-b;
}
else if(op == '*')
{
cout<<"Mul = "<<a*b;
}
else if(op == '/')
{
if(b == 0)
{
cout<<"Cannot divide by zero";
}
else
{
cout<<"Div"<<a/b;
}
}
else
{
cout<<"Invalid Operator";
}
}
Hope You will learn something new here.
Write a Program to Make a Simple Calculator in C++ Programming Language
Watch the Video.