Hello I dont understand this code, why output is 3? That's wrong. ( 10/3=3.3333333333) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hello I dont understand this code, why output is 3? That's wrong. ( 10/3=3.3333333333)

#include <iostream> using namespace std; int main() { int x = 10 / 3; cout << x; return 0; }

12th Feb 2017, 7:51 PM
Hussain Madadi
11 Answers
+ 1
answer is variable type you determined. the best solution to such a problems. using "auto" type variable and typing exactly float style
13th Feb 2017, 2:44 AM
Arif Soylu
Arif Soylu - avatar
+ 11
#include <iostream> using namespace std; int main() { float x = 10; cout << x/3; return 0; }
12th Feb 2017, 8:47 PM
Hatsy Rei
Hatsy Rei - avatar
+ 9
it is not wrong. x is integer than the result must be integer
12th Feb 2017, 8:50 PM
L Boudhar
L Boudhar - avatar
+ 8
because x is an int, not a float (decimal)
12th Feb 2017, 7:59 PM
SirLucidus
SirLucidus - avatar
+ 6
10/3 is 3.33333 but your combiner doesn't show the decimal.
13th Feb 2017, 2:41 AM
keivan Ipchi
keivan Ipchi - avatar
+ 3
try making x a float instead
12th Feb 2017, 7:59 PM
SirLucidus
SirLucidus - avatar
+ 1
I tried but it's same answer. #include <iostream> using namespace std; int main () { float x = 10 / 3; cout << x; return 0; // output is "3" // 😨 }
12th Feb 2017, 8:26 PM
Hussain Madadi
+ 1
I've got it: use 10.0 and 3.0 instead of 10 and 3. Any Luck?
12th Feb 2017, 8:36 PM
SirLucidus
SirLucidus - avatar
0
😁👍👍👍👍
12th Feb 2017, 8:41 PM
Hussain Madadi
0
Did 10.0 and 3.0 work for you?
12th Feb 2017, 8:59 PM
SirLucidus
SirLucidus - avatar
0
try this one #include <iostream> using namespace std; int main(){ float x,y; cout << x/y; return 0; }
16th Feb 2017, 2:33 AM
Amrin Jaffni
Amrin Jaffni - avatar