1.

Consider the following statements in C++ 1) cout<<41/2; 2) cout<<41/2.0; Are this two statements give same result? Explain?

Answer»

This two statements do not give same results. The first statement 41/2 gives 20 instead of 20.5. The reason is 41 and 2 are integers. If two operands are integers the result must be integer, the real part must be truncated. To get floating result either one of the operand must be float. So the second statement gives 20.5. The reason is 41 is integer but 2.0 is a float.



Discussion

No Comment Found

Related InterviewSolutions