﻿ C++ program example: calculator

# 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);
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;
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;
}
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;
} Harshdeep Singh Yadav Kumarwhat the heaven...!!!2016-05-12 #anonymousCouldn't you just make it simple and do it like this: #include 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 KumarHere you will find better code 2015-03-19 Nitin KumarHere you will find better code 2015-03-19