1.

Evaluate the following C++ expressions where x, y, z are integers and m, n are floating point numbers. The value of x = 5, y = 4 and m = 2.5;1. n = x + y / x; 2. z = m * x + y; 3. z = (x++) * m + x;

Answer»

1. n = x + y / x;

= 5 + 4/5

= 5 + 0 (both x and y are int type. Therefore only integer part of quotient is considered)

= 5

2. z = m * x + y;

= 2.5 * 5 + 4 (m is float type, so x value is promoted to float [implicit conversion])

= 12.5 + 4 ‘

= 16 (2 is int type. So ‘.2’ , the fractional part is discarded)

3. z = (x++) * m + x;

= 5*2.5 + x

= 12.5 + 5

= 18 (z is int type, therefore the fractional part is removed, x is incremented after the addition)



Discussion

No Comment Found