+ 3
int x = 1/ 3; cout << x; // Outputs 0 . Why?
9 odpowiedzi
+ 14
x is an integer. By default, the fractional/decimal part will be taken off, basically resulting in a floor. So .333333... The decimal will be taken off and now it is 0.
+ 13
x is an integer, it cant store decimal places. 1/3 gives 0.33333... , and it cant store that, hence it rounded down to 0.
+ 11
integers dont round. they truncate. The decimal places go to a digital void never to be seen again
edit:
example. we store 4.99 into an integer.
we look at it and it says it is 4. Why? because integers cut off everything after the decimal place
+ 9
X is an integer... Because of that you get only the integer part of 0.33333.... It is 0.
+ 5
Because x is int and the integer part of 1/3 is 0. If you want to print 0.333 ... use variables of floating type.
+ 2
actually 1/3 result is 0.3333333..but return type of x is declared int. so,output is 0.
+ 1
use float x instead of int x
0
use float not int.
for ex:
float x;
x = 1/3;
cout<< x <<endl;
0
0