Answer to Question #333099 in C++ for Padi

Question #333099

Calculator should be able to handle the five basic mathematics operations – +,-,*and divide and modulus – on two input values. 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 Submit both your program and output.


1
Expert's answer
2022-04-29T08:21:28-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;
}

Output:

Please enter the first float value: 35.5
Please enter the second float value: 20.4
Please enter the operation required : +
The sum of 35.5 and 20.4 is 55.90

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