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

7th Dec 2021, 4:25 PM
â€ŽÙ…ÛŒÙ… Ø´ÛŒÙ†â€Ž
+ 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.
7th Dec 2021, 4:28 PM
AÍ¢J
+ 3
You could also use double b = 9.0/7.0;
7th Dec 2021, 4:38 PM
+ 2
7th Dec 2021, 4:29 PM
â€ŽÙ…ÛŒÙ… Ø´ÛŒÙ†â€Ž
+ 2
â€ŽÙ…ÛŒÙ… Ø´ÛŒÙ†â€Ž In Java too you will get same result but 1.0 instead of 1
7th Dec 2021, 4:38 PM
AÍ¢J
+ 2
You could also use double b = 9.0/7; or double b = 9/7.0;
7th Dec 2021, 5:24 PM
SoloProg
+ 2
Because division of two integers is always an integer even if you declare it as double or float.
8th Dec 2021, 9:44 AM
Abhishek Pandey
+ 2
Use this instead, double = 9.0 / 7.0
8th Dec 2021, 9:45 AM
Abhishek Pandey