Answer to Question #189667 in C++ for Sydney Nthane

Question #189667

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. Your program should have the following structure:  Ask the user to enter two float variables named var1 and var2  Ask the user to enter a character variable named operation to represent the operation to be performed on the two variables.  Perform the appropriate operation by using if-statements  The output must be given in fixed-point notation with two digits after the decimal point. A typical run is displayed below: Please enter the first float value: 35.6 Please enter the second value: 24.12 Please enter the operation required : + The sum of 35.6 and 24.12 is 59.72 


1
Expert's answer
2021-05-08T07:25:42-0400
#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;


int main()
{
    cout << "Please enter the first float value: ";
    float var1;
    cin >> var1;


    cout << "Please enter the second float value: ";
    float var2;
    cin >> var2;


    cout << "Please enter the operation required : ";
    char operation;
    cin >> operation;


    cout << endl;
    if(operation == '+')
    {
        float sum = var1 + var2;
        cout << "The sum of " << var1 << " and " << var2 << " is " << setprecision(2) << fixed << sum;
    }
    else if(operation == '-')
    {
        float difference = var1 - var2;
        cout << "The difference of " << var1 << " and " << var2 << " is " << setprecision(2) << fixed << difference;
    }
    else if(operation == '*')
    {
        float multiplication = var1 * var2;
        cout << "The multiplication of " << var1 << " and " << var2 << " is " << setprecision(2) << fixed << multiplication;
    }
    else if(operation == '/')
    {
        float division = var1 / var2;
        cout << "The division of " << var1 << " and " << var2 << " is " << setprecision(2) << fixed << division;
    }
    else
    {
        float reminder = fmod(var1,var2);
        cout << "The reminder of " << var1 << " and " << var2 << " is " << setprecision(2) << fixed << reminder;
    }


    return 0;
}

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
APPROVED BY CLIENTS