You are requested to write a very simple calculator. Your calculator should be able to handle the five basic mathematic
operations – add, subtract, multiply, divide and modulus – on two input values.
/******************************************************************************
*Basic C++ Calculator program
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//Functions declaration
double addition(double a, double b);
double subtraction(double a, double b);
double division(double a, double b);
double multiplication(double a, double b);
double modulusOp(double a, double b);
int main()
{
double num1, num2; //Using double to accomodate ints, floats and double types
char mathOperator; //Hold operator
cout<<"==========Hello am a basic math calculator I can add, subtract, devide, multiply and find remainder after division of two numbers============="<<endl;
//Prompt for numbers
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Enter the operation symbol (+, -, /, %, or * : ";
cin>>mathOperator;
if(mathOperator == '+')
{
cout<<"Answer is : "<<addition(num1, num2);
}
else if(mathOperator == '-')
{
cout<<"Answer is : "<<subtraction(num1, num2);
}
else if(mathOperator == '*')
{
cout<<"Answer is : "<<multiplication(num1, num2);
}
else if(mathOperator == '/')
{
cout<<"Answer is : "<<division(num1, num2);
}
else if(mathOperator == '%')
{
cout<<"Answer is : "<<modulusOp(num1, num2);
}
return 0;
}
//Functions definition
double addition(double a, double b)
{
return a + b;
}
double subtraction(double a, double b)
{
return a - b;
}
double division(double a, double b)
{
return a / b;
}
double multiplication(double a, double b)
{
return a* b;
}
double modulusOp(double a, double b)
{
return fmod(a, b);
}
Comments
Leave a comment