Computer Applications
A student needs to calculate the average of three numbers x
, y
, and z
. Which is the correct Java expression for this calculation?
Java Operators
2 Likes
Answer
(x+y+z)/3.0
Reason — Division (/
) has higher precedence than addition (+
), so without parentheses, the division would happen before the addition. Using parentheses ensures that the sum x + y + z
is calculated first, and then divided by 3.0
. Using 3.0
ensures the division is performed as floating-point, avoiding integer truncation. This guarantees the correct calculation of the average.
Answered By
3 Likes
Related Questions
Write Java expression for:
Write the Java expression for the following:
Identify the operator that gets the highest precedence while evaluating the given expression:
a + b % c * d - e
- +
- %
- -
- *
Evaluate the given expression when the value of a=2 and b=3
b*=a++ - ++b + ++a; System.out.println("a= "+a); System.out.println("b= "+b);