How to Make a Calculator in C++

This Program is very simple you just need to compute the two integers with different Mathematical Operations. In our Solution we did it with Switch Statement which is very common in C++, You can also solve it with Using Functions but for beginners its good to introduce them the basic of C++ as simple as possible .

Source C ode  

#include <iostream>        
#include <iomanip>   
#include <windows.h>
using namespace std;

int main()
 {
    int choice;
   do
   {
    int number1 =0;   //intilize a Variable that is recieve number 1
    int number2 =0;  //intilize a Variable that is recieve number 2
    int result =0;
    cout<<endl<<setw(47)<<"************* Math Calculator ***************"<<endl;
    cout<<endl<<"What Do you Want"<<endl;     // Here is Given Menu Which is Select on Requirement
    cout<<endl<<setw(25)<<"1 : Add"<<endl;
    cout<<setw(30)<<"2 : Subtract"<<endl;
    cout<<setw(36)<<"3 : Multiplication"<<endl;
    cout<<setw(30)<<"4 : Division"<<endl;
    cout<<setw(27)<<"0 : Exist"<<endl;
    cin>>choice;
    if(choice==1)
    {
     cout<<endl<<"Enter Number_1:";
     cin>>number1;          //Reciever input value for number 1
     cout<<endl<<"Enter Number_2:";
     cin>>number2;   //Reciever input value for number 2
     result = number1 + number2;    // Here is both numbers are added
     cout<<endl<<endl<<setw(30)<<"Answer : "<<result<<endl;  // Result is Displayed in here
     Sleep(5000);  // Waiting for 5 second
    } 
    if(choice==2)
    {
     cout<<endl<<"Enter Number_1:";
     cin>>number1;   //Reciever input value for number 1
     cout<<endl<<"Enter Number_2:";
     cin>>number2;   //Reciever input value for number 2
     result = number1 - number2;    // Here are  numbers subtraction 
     cout<<endl<<endl<<setw(30)<<"Answer : "<<result<<endl;  // Result is Displayed in here
     Sleep(5000);  // Waiting for 5 second
    }
    if(choice==3)
    {
     cout<<endl<<"Enter Number_1:";
     cin>>number1;   //Reciever input value for number 1
     cout<<endl<<"Enter Number_2:";
     cin>>number2;   //Reciever input value for number 2
     result = number1 * number2;  // Here is numbers are multiplied 
     cout<<endl<<endl<<setw(30)<<"Answer : "<<result<<endl;  // Result is Displayed in here
     Sleep(5000);  // Waiting for 5 second
     
    }
    if(choice==4)
    {
     double num1, num2;
     cout<<endl<<"Enter Number_1:"; 
     cin>>num1;  //Reciever input value for number 1
     cout<<endl<<"Enter Number_2:";
     cin>>num2;  //Reciever input value for number 2
     if(num2==0)  // Because Zero can't Diveide by any words
     {
      cout<<endl<<"Can't Devide by Zero "<<endl;  
      Sleep(5000);  // Waiting for 5 second
     }
     else
     {
     double resul = num1 / num2;  // Here is Number 1 is Divided on Number 2
     cout<<endl<<endl<<setw(30)<<"Answer : "<<resul<<endl;   // Result is Displayed in here
     Sleep(5000);  // Waiting for 5 second
     }
     
    }
    
   }while(choice!=0);
 return 0;
}

Output of the Program

When you enter the first Option
Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment