In Java,
what will be the output of the following code and how?
double x=2.9,y=2.5;
(SOP is abbreviated form of System.out.println)
SOP(Math.min(Math.floor(x),y));
SOP(Math.max(Math.ceil(x),y));
Explain how...
Answers
Hey Mate!
Here's the solution:
x = 2.9 and y = 2.5
Math.min( ) returns the minimum(or the smallest) number among the 2 numbers given(In this case, the numbers are Math.floor(2.9) and 2.5) .
Math.max( ) returns the maximum(or the largest) number among the 2 numbers given(In this case, the numbers are Math.ceil(2.9) and 2.5).
Math.floor( ) returns the number given by rounding it downwards. For example, Math.floor(3.2) returns 3.0 and Math.floor(5.6) returns 5.0.
Math.ceil( ) can be called the opposite of Math.floor( ). It returns the number given by rounding it upwards. For example, Math.ceil(3.2) returns 4.0 and Math.ceil(5.6) returns 6.0
In the given question, Math.floor(x) will return 2.0 And the minimum among 2.0 and 2.5 is 2.0 and hence it gets displayed. Math.ceil(x) returns 3.0 and the maximum among 3.0 and 2.5 is 3.0 which gets displayed.
So, the output should be as follows:
2.0
3.0
Hope it helps!
And all the best for your exam!
Explanation:
the output of the programming segment is:
2.0
3.0