What will be the output of the following C++ code?

#include <iomanip>
   #include <iostream>
   using namespace std;
   int main()
   {
       cout << setprecision(17);
       double d = 0.1;
       cout << d << endl;
       return 0;
   }

a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error

1 thought on “What will be the output of the following C++ code?”

  1. b
    Explanation: The double had to truncate the approximation due to its limited memory, which resulted in a number that is not exactly 0.1.
    Output:
    $ g++ float2.out
    $ a.out
    0.10000000000000001

Leave a Comment