Simple Calculator In C++

Using If Conditions and Arithmetic Operators Simple Calculator In C++

Posted by Zohaib Qazi on may 15, 2017

Simple Calculator In C++

After reading this post you will come to understand that How to make a simle calculator in C++. And while coding this simple calculator you will get to know that all if condtions and arithmetic Operators such as Addition, Subtraction, Division and Multiplication are being used. However, clear your concept by reading this post and watching our Balochi video which is placed below.

Descriptions Process

First of all take 4 variables. Which are declared below :
double num1, num2;
char opt;
double result;

Then Take three cout which will be display on screen for instructions guidance and take three input cin. The two inputs are for storing the num1 and num2 values. and the opt is for operators like ( + , *, /, - ). Which are given below :

cout<<"Enter the number One : ";
cin>>num1;
cout<<"Enter the number Two : ";
cin>>num2;
cout<<"Enter the Operator (* , + , /, - ): ";
cin>>opt;

However, after all of those above take if conditions and put your condtion on. If condition one is not true then second condtion is checked, and if second condtion is not true then other conditions are checked step by step until the condition gets true or the program will terminates.And The Code is given below:

if (opt == '+') {
result = num1 + num2;
cout<<"Addition : " << result << endl ;
}
else if (opt == '-'){
result = num1 - num2;
cout<<"Subtraction : " << result << endl ;
}
else if (opt == '*'){
result = num1 * num2;
cout<<"Multiply : " << result << endl ;
}
else if (opt == '/'){
result = num1 / num2;
cout<<"Division : " << result << endl ;
}
else {
cout<<"Wrong Input !!! " << endl ;
}

Simple Calculator In C++ Video in Balochi

Simple Calculator C++ code by balochprogrammer


#include < iostream >
using namespace std;
int main()
{
double num1, num2;

char opt;
double result;

cout<<"Enter the number One : ";
cin>>num1;
cout<<"Enter the number Two : ";
cin>>num2;
cout<<"Enter the Operator (* , + , /, - ): ";
cin>>opt;


if (opt == '+') {
result = num1 + num2;
cout<<"Addition : " << result << endl ;
}
else if (opt == '-'){
result = num1 - num2;
cout<<"Subtraction : " << result << endl ;
}
else if (opt == '*'){
result = num1 * num2;
cout<<"Multiply :" << result << endl ;
}
else if (opt == '/'){
result = num1 / num2;
cout<<"Division : " << result << endl ;
}
else {
cout<<"Wrong Input !!! " << endl ;
}
return 0 ;
}