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
1 Like
Related Questions
A store has purchased some cola cans of various brands. It purchased 50 cans @ ₹15 per can, 30 cans @ ₹20 per can and 40 cans @ ₹21 per can. Write a Java program to compute how much amount did the store pay.
If the value of basic=1500, what will be the value of tax after the following statement is executed?
tax = basic > 1200 ? 200 :100;
Write a difference between unary and binary operator.
A student executes the following code to increase the value of a variable ‘x’ by 2.
He has written the following statement, which is incorrect.
x = +2;
What will be the correct statement?
A. x +=2;
B. x =2;
C. x = x +2;- Only A
- Only C
- All the three
- Both A and C