 # C++ Programming Code Examples

## C++ > Mathematics Code Examples

### C++ calculator code for beginner

``` C++ calculator code for beginner Write a simple C++ program which have four different functions for basic operations like addition, subtraction, division and multiplication. Program should be menu based asking user to enter option and if user enters a wrong option a proper message to enter right option should be displayed. Program has four different function for each calculator operation do while loop is used to display menu options if else statement is used to check option Program has two variables on which all basic operations are applied Every functions receives two parameters and return result #include <iostream> using namespace std; int addition (int number1, int number2); int subtraction (int number1, int number2); int division (int number1, int number2); int multiplication(int number1, int number2); int main() { char option; int number1, number2, result; cout<<"\n\t\t\tSimple calculator"<<endl; do { cout<<"Enter + for Addtion\nEnter - for Subtraction\n Enter / for Division\nEnter * for Multiplication\n Enter E to EXIT\n"; cout<<"\n\t\t\tEnter Option: "; cin>>option; if(option!='e'&&option!='E') { cout<<"\n\t\t\tEnter First Number: "; cin>>number1; cout<<"\n\t\t\tEnter Second Number: "; cin>>number2; } if(option!='+'&&option!='-'&&option!='/' &&option!='*'&&option!='e'&&option!='E') { cout<<"\n\t\t\tselect the valid option\n"; } else if(option=='+') { result=addition(number1,number2); cout<<"\n\t\t\t"<<number1<<" + "<<number2<<" = "<<result<<endl; } else if(option=='-') { result=subtraction(number1,number2); cout<<"\n\t\t\t"<<number1<<" - "<<number2<<" = "<<result<<endl; } else if(option=='/') { result=division(number1,number2); cout<<"\n\t\t\t"<<number1<<" / "<<number2<<" = "<<result<<endl; } else if(option=='*') { result=multiplication(number1,number2); cout<<"\n\t\t\t"<<number1<<" X "<<number2<<" = "<<result<<endl; } }while(option!='e'&&option!='E'); cout<<"\n\t\t\tProgram EXIT Successfully.......\n\t\t\t"; return 0; } int addition(int number1, int number2) { return (number1+number2); } int subtraction(int number1, int number2) { return (number1-number2); } int division(int number1, int number2) { if(number2==0) { cout<<"\n\t\t\tDivide by ZERO not allowed :"<<endl; return 0; } return (number1/number2); } int multiplication(int number1, int number2) { return (number1*number2); } ``` 