176k views
5 votes
Write a program that displays the result of (9.5*4.5 - 2.5*3) / (45.5-3.5) rounded to 6 digits after the decimal mark.

1 Answer

4 votes

Answer:

The c++ program to compute and display the result of the given expression. The result is displayed having six numbers after the decimal point.

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

// floating variables to store all values

float a = 9.5, b = 4.5, c = 2.5, d = 3, e = 45.5, f = 3.5;

// floating variables to store all sub results

float result, res1, res2, res3;

res1 = ( a * b );

res2 = ( c * d );

res3 = ( e - f );

result = ( res1 - res2 ) / res3;

// setprecision( 6 ) displays only 6 digits after the decimal point

cout << " The result for the given expression, rounded to 6 digits after the decimal mark, is " << setprecision(6) << result << endl;

return 0;

}

OUTPUT

The result for the given expression, rounded to 6 digits after the decimal mark, is 0e 39286

Step-by-step explanation:

1. The variables are declared and initialized to hold all the values to be used in the expression.

float a = 9.5, b = 4.5, c = 2.5, d = 3, e = 45.5, f = 3.5;

2. The variables are also declared to hold the sub result of each bracket.

float result, res1, res2, res3;

3. The final answer is stored in the variable result.

result = ( res1 - res2 ) / res3;

4. All the variables are taken as floating numbers and not double data type since only 6 digits are needed after the decimal point.

5. The answer is displayed up to six decimal places using the setprecision() method. Since, 6 decimals are needed in the final answer, 6 is used with the setprecision() method. The iomanip header file is included for this method.

setprecision( 6 ) << result

User Danielnixon
by
8.6k points