C Programming :: Declarations and Initializations - Discussion
11 / 54
What is the correct way to round off x, a float, to an int value?
Ay = (int)(x + 0.5);
By = int(x + 0.5);
Cy = (int)x+ 0.5;
Dy = (int)((int)x + 0.5);
Show Explanation
Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.
y = (int)(x 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)
Asked In ::
Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.
y = (int)(x 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)
Read Full Answer
Report Error
Please Login First Click Here
Option A is the correct way to typecast a float type value into an integer type. Also, while typecasting it is important to correctly use the parenthesis or else we may get inappropriate output. Option A is most appropriate than the others because first we are computing the expression i.e, x+0.5 and we put it inside a parenthesis, after which we typecast the resulting float value into integer by specifying (int), hence the resulting float type value will be rounded off to the nearest integer.
Read Full Answer
Report Error
Please Login First Click Here