Answer to Question #197238 in C++ for mcdonald mthiyane

Question #197238

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.


1
Expert's answer
2021-05-23T14:38:13-0400
/******************************************************************************
    *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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog