QUESTION 75 · Expressions and conversions
Why cast before integer division when a fractional result is needed?
2 marksAnswer
Cast an operand before division to obtain a fractional result from integer values. Casting after integer division cannot restore the discarded fraction.
- For total = 7 and count = 2, (double)total / count gives 3.5.
- (double)(total / count) gives 3.0 because 7 / 2 first gives integer 3.