Why in c++ double doesn't work if I write:
int a, b;
double c;
a=11;
b=2;
c=a/b;
right answer according to me should be 5,5 but it writes just 5 anyone knows how to fix this?
Why in c++ double doesn't work if I write:
int a, b;
double c;
a=11;
b=2;
c=a/b;
right answer according to me should be 5,5 but it writes just 5 anyone knows how to fix this?
A division of an int
by another int
yields an int
. This occurs before the quotient is assigned to the double
variable.
To fix this, cast at least one of the int
values to a double
. In other words, change this:
c=a/b;
to this:
c = static_cast<double>(a) / b;