Learn C++ - Mini Command base Calculator to Add,Subtract,Multiply and Divide

Learn to make a mini Command base Calculator, which takes two integer operands and one operator from user , performs the operation and then prints the result.

Before starting this you should have basic knowledge of C++ I/O operators


Program:

#include<iostream.h>
#include<conio.h>
void main
{
clrscr();

int a,b,c; char sign;
cout<<"Enter First number:"<<endl;
cin>>a;
cout<<"Enter Second Number"<<endl;
cin>>b;
cout<<"Press + to Add "<<endl<<"Press - to subtract"<<endl<<"Press * to multiply"<<endl<<"Press / to Divide"<<endl;
cin>>sign;

if(sign=='+')
{c=a+b;
cout<<"Sum of "<<a <<"and "<<b<<" is :"<<c;
}

else if(sign=='-')
{c=a-b;
cout<<"Difference  of "<<a <<"and "<<b<<" is :"<<c;
}

else if(sign=='*')
{c=a*b;
cout<<"Product of "<<a <<"and "<<b<<" is :"<<c;
}


else if(sign=='/')
{c=a/b;
cout<<"Division of "<<a <<"and "<<b<<" is :"<<c;
}

else {cout<<"Invalid operator";}
getch();}


Explanation:
  • endl will bring the user to next line , \n can be used to perform same function
  • Every if statement should have ending else .
  • if else are loops and do not require ";" at the end.
  • + , - ,*, / are characters , to assign them , we need to enclose them in single quotes(')