LearnFrenzy
Sign in Take a free test

Input/Output with cin and cout · Predict the Output · Question 3

setprecision in default, fixed and scientific notation Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following C++ program?

main.cpp
#include <iomanip>
#include <iostream>

int main() {
    double x = 12.3456;
    std::cout << std::setprecision(2) << x << ' '
              << std::fixed << x << ' '
              << std::scientific << x << '\n';
    return 0;
}
  • A 12 12.35 1.23e+01 Correct
  • B 12.35 12.35 1.23e+01
  • C 12 12.34 1.2e+01
  • D 1.2e+01 12.35 1.23e+01
Correct Answer: Option A

Default notation: 2 significant digits → 12. fixed: 2 decimals → 12.35. scientific: 2 decimals in the mantissa → 1.23e+01.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links