Simple Calculator Using C++ Programming Language

Calculator:  

A device which performs arithmetic operations and simple calculator is one which performs addition , subtraction , multiplication and division mathematical operation
  • Addition    
  • Subtraction
  • Multiplication 
  • Division 
First of all you have to know that how a simple calculator works. Simple calculator means that it can perform basic mathematical operations like summation , subtraction , multiplication and division.Now there are four operations and all that you have to know is the common thing between these mathematical operations.In all these operations , calculator take three values from the user and shows the result according to operation you defined like summation etc.Three values  means two numbers and mathematical operation.
simple calculator

Algorithm:

"For all above operations , need to check which mathematical operation is going to be performed."

In above all operation , user have to enter two numbers , so two variables with type double. For mathematical operation like + , - , * , / requires variable of type char . For the result of two numbers also requires one variable of type double.
Now if you want to check operation , i am going to use switch statement , if you don't know about this statement , then click on it, it is very easy.

Now in order to perform operations again and again , there is no need to go while compile and execute your code every time. For this i am using do while loop, if you don't know about this loop click on it, it is very easy or visit my C++ lectures page.

Recommended Articles:


Program code:

Note: Program code is very easy, i used comments with the every section so that you can under stand quickly

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
double num1 , num2 ;     // input variables
char operation ;        // for mathematical operator
char redo ;             // for next calculation
double ans=0 ;         //  Result
cout<<endl<<endl;   // endl ==> next line
cout<<"********************************************"<<endl;
cout<<"             Simple Calculator              "<<endl;
cout<<"********************************************"<<endl<<endl<<endl;
do
{
   cout<<"  Enter First number  =   ";
   cin>>  num1;
   cout<<endl;
   
cout<<"  Enter operation +  , - , * , /  =   ";
   cin >> operation;
   cout<<endl;
   cout<<"  Enter Second number  =  ";
   cin>>  num2;
   cout<<endl<<endl;
   // For different mathematical operations
   switch(operation)
   {
   //Addition
   case '+':
    ans = num1 + num2 ;
    cout<<"  Addition of two numbers is  =   "<< ans ;
    break;
   //Subtraction
   case '-':
    ans = num1 - num2 ;
    cout<< "  Subtraction of two numbers is  =  "<< ans;
    break;
        //Multiplication
   case '*':
   ans = num1 * num2 ;
   cout<<"  Multiplication of Two numbers is  =  "<< ans;
   break;
   //Division
   case '/':
   ans = num1 / num2 ;
   cout<<"  Division of two numbers is  =  "<< ans ;
   break;
   //other than + - * /
   default:
  cout<<endl;
  cout<< "  invalid operation  " << endl;
  }
  
    cout<<endl<<endl<<"********************************************"<<endl<<endl;
cout<<endl<<endl<< "  Enter y or Y for next Calculation =  ";
    cin>>redo;
    cout<<endl<<endl;
        
    } while(redo == 'y' || redo == 'Y');
    
getch();
}

Output:
The output of above program will be like this

Click on image to enlarge it.

Hope that you will find this helpful.

1 comment:

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.