C++ Cout floating point problem

c++floating-point

#include <iostream>
using namespace std;
int main()
{
        float s;
        s = 10 / 3;
        cout << s << endl;
        cout.precision(4);
        cout << s << endl;
        return 0;

}

Why the output does not show 3.333 but only 3 ??

Best Solution

because you are doing integer division with s = 10 / 3

Try

s = 10.0f / 3.0f