When we use operation 15/2 ,how can we get output as 7.5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When we use operation 15/2 ,how can we get output as 7.5

Please

5th May 2020, 4:30 PM
Somendra
4 Answers
+ 5
I assume you are talking about C++, which you have started, or a comparable language. If you write 15/2 in your code, both these numbers are ints. That means they have no decimals. So 7.5 becomes just 7. Even if you write... double d = 15/2; ... this doesn't help, because the result of two ints is first calculated, then converted to double. Result: 7.0. The trick is to make one of the operands a double literal. So write either 15/2.0 or 15.0/2.
5th May 2020, 4:43 PM
HonFu
HonFu - avatar
+ 5
HonFu We can just do typecast like this int a = 15; int b = 2; cout << (double) a/b;
5th May 2020, 4:49 PM
A͢J
A͢J - avatar
+ 4
Somendra mention the language please. in Java we can do like this int a = 15; int b = 2; double d = 0.0; d = (double) a / b; System.out.println(d); In C++ ----------- int a = 15; int b = 2; cout << (double) a/b;
5th May 2020, 4:37 PM
A͢J
A͢J - avatar
+ 1
Yeah, AJ, and that.
5th May 2020, 5:06 PM
HonFu
HonFu - avatar