0
double in cpp
https://code.sololearn.com/coaM9cpWltrq/?ref=app Why ìn cpp: double b=9/7; //its always 1; cout<<b; But double b=9; //its accurate 1.28571428 cout<<b/7; Both are DOUBLE With Respect
7 Answers
+ 3
میم شین
9 and 7 are int not double.
You need to cast one of them with double to get in decimal:
double b = (double) 9 / 7;
In second case b is double so 9 will be treated as double.
+ 3
You could also use double b = 9.0/7.0;
+ 2
Thx Mr. A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
+ 2
میم شین
In Java too you will get same result but 1.0 instead of 1
+ 2
You could also use
double b = 9.0/7;
or
double b = 9/7.0;
+ 2
Because division of two integers is always an integer even if you declare it as double or float.
+ 2
Use this instead, double = 9.0 / 7.0