Answer to Question #297110 in C++ for junior

Question #297110

Write a program that prints the value of 120.456789 rounded to the nearest digit, tenth, hundredth, thousandth, and ten-thousandth. [Hint: This should make use of formatted output.]


1
Expert's answer
2022-02-13T15:17:23-0500
#include <iostream>
#include <iomanip>
using namespace std;


int main() {
    double x = 120.456789;


    cout << fixed;
    for (int i=0; i<=4; i++) {
        cout << setprecision(i) << x << endl;
    }


    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