+ 3

int x = 1/ 3; cout << x; // Outputs 0 . Why?

19th May 2017, 12:22 AM
Abdulrahim Idris
Abdulrahim Idris - avatar
9 Antworten
+ 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.
19th May 2017, 12:29 AM
J.G.
J.G. - avatar
+ 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.
19th May 2017, 12:28 AM
Wen Qin
Wen Qin - avatar
+ 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
19th May 2017, 12:46 AM
jay
jay - avatar
+ 9
X is an integer... Because of that you get only the integer part of 0.33333.... It is 0.
19th May 2017, 12:37 AM
Prabhanu Gunaweera
Prabhanu Gunaweera - avatar
+ 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.
19th May 2017, 12:30 AM
Daniel Vasquez
Daniel Vasquez - avatar
+ 2
actually 1/3 result is 0.3333333..but return type of x is declared int. so,output is 0.
25th Dec 2017, 9:56 AM
KOGANTI POOJA SRAVANI
KOGANTI POOJA SRAVANI - avatar
+ 1
use float x instead of int x
20th May 2017, 9:16 AM
Asva Abadila
Asva Abadila - avatar
0
use float not int. for ex: float x; x = 1/3; cout<< x <<endl;
20th May 2017, 10:20 AM
Ceejay jhao
0
0
15th Dec 2017, 8:13 PM
2kool 4life
2kool 4life - avatar