The following is a very simple c++ program, that i believe should show the value of a variable x. since x = 1/3, i believe that the out put should be 0.3333, however, the output i get is x=0.
can anybody tell me why?
#include <iostream>
using namespace std;
int main()
{
double x=10, dummy;
x = (1/3);
cout << "X : " << x << endl;
cout << "Exit" << endl;
cin >> dummy;
return 0;
}
x = 1/3; then it will interpret 1 and 3 as both integers, and put the result of the expression accordingly. To get it to display 0.333333 you need to change your code to
x = (1/3.0); or alternatively
x = (1.0/3);That way at least one of the values will be interpreted as floating point types, and so you’ll get a floating point answer.