C++ program example: calculator


Calculator

This is the C++ calculator example program. It is basic calculator that can sum, subtract, multiply, and divide two numbers.

#include <cstdlib>
#include <iostream>
double sum(double a,double b);
void menu();
double subtract(double a,double b);
double multiply(double a,double b);
double divide(double a,double b);
using namespace std;
int main(int argc, char *argv[])

{

  int c;
  double r,a,b;
  menu();
  cout<<"Enter your choice: "<<endl;
  cin>>c;
  switch(c){
         case 1:cout<<"Enter a: ";cin>>a;
                   cout<<"Enter b: ";cin>>b;
                   r=sum(a,b);
                   cout<<"Result:"<<r<<endl;break;
         case 2:cout<<"Enter a: ";cin>>a;
                   cout<<"Enter b: ";cin>>b;
                   r=subtract(a,b);
                   cout<<"Result:"<<r<<endl;break;
         case 3:cout<<"Enter a: ";cin>>a;
                  cout<<"Enter b: ";cin>>b;
                  r=multiply(a,b);
                 cout<<"Result:"<<r<<endl;break;

          case 4:cout<<"Enter a: ";cin>>a;
                  cout<<"Enter b: ";cin>>b;
                  r=divide(a,b);
                  cout<<"Result:"<<r<<endl;break;
          default:cout<<"Invalid choice"<<endl;

}

       system("PAUSE");
       return EXIT_SUCCESS;
}

double sum(double a,double b){
       double result_sum= a+b;
       return result_sum;
}


double subtract (double a, double b){
       double result_subtract =a-b;
       return result_subtract;
}
double multiply (double a, double b){
      double result_multiply =a*b;
      return result_multiply;

}

double divide (double a, double b){
       double result_divide =a/b;
       return result_divide;
}
void menu(){
      cout<<"Please choose an option (1,2,3,4):";
      cout<<"1. Sum"<<endl;
      cout<<"2. Subtract"<<endl;
      cout<<"3. Multiply"<<endl;
      cout<<"4. Divide"<<endl;
}






Comments

Harshdeep Singh Yadav Kumar comment

 Harshdeep Singh Yadav Kumar

what the heaven...!!!


2016-05-12
#anonymous comment

 #anonymous

Couldn't you just make it simple and do it like this: #include <iostream>
using namespace std;

int main()
{

float a , b;
int process;
cout << "Hello. I am a calculator. Please do not enter any letters while using me. Thanks." << endl;
while (true){
cout << "Type in a number (0 to exit): ";
cin >> a;
if (a == 0)
return 0;
else
cout << " Enter a second number: ";
cin >> b;
cout << "Would you like to add(1), subtract(2), multiply(3), or divide(4) your numbers? ";
cin >> process;
if (process == 1)
cout << "The sum of the numbers is " << a + b << endl;
else if (process == 2)
cout << "The difference of the numbers is " << a - b << endl;
else if (process == 3)
cout << "The product of the numbers is " << a * b << endl;
else if (process == 4)
cout << "The quotient of the numbers is " << a / b << endl;
else if (process != 1 , 2 , 3 , 4)
cout << "Please type a number 1-4." << endl;
}
}
This provides a program that acts as a calculator until the user types in 0.


2015-07-08
Nitin Kumar comment

 Nitin Kumar

<a href="http://www.easycppcodes.com/cpp-codes/cpp-program-make-calculator/">Here you will find better code </a>


2015-03-19
Nitin Kumar comment

 Nitin Kumar

<a href="http://www.easycppcodes.com/cpp-codes/cpp-program-make-calculator/">Here you will find better code </a>


2015-03-19



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.