0
When we use operation 15/2 ,how can we get output as 7.5
Please
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.
+ 5
HonFu We can just do typecast like this
int a = 15;
int b = 2;
cout << (double) a/b;
+ 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;
+ 1
Yeah, AJ, and that.