Computer Science, asked by pleaseanswer7736, 1 year ago

What is the value of "d" after this line of code has been executed? double d = math.round ( 2.5 + math.random( ));?

Answers

Answered by Astrobolt
1
Just a note, your program will actually not compile due to the syntax errors, but I assume that you just want to know the output, hence you haven't considered writing the proper syntax in the question.

The math.random() method by default generates any number between 0 and 1.

The math.round() method rounds off numbers and returns an integer value.

To solve this, we need to look at first how math.round() does it's job.

What ever the value say X in the math.round() method is first added to 0.5

Then the "floor" or the largest integer that is less than (X + 0.5) is returned and typecasted as an int.

So let's take a few examples for just the round method first.

math.round(14.9) will return 15
math.round(-13.2) will return -13

Now then since math.random can return any value between 0 and 1,

The input of math.round will be between 2.5 and 3.5

Judging from the inputs we can say that the math.random method will return 3 as it's output.

Hence the output is 3.
Similar questions