recategorized by
1,775 views
3 votes
3 votes

What is the correct way to round off $x$, $a$ $\text{float}$ to an $\text{int}$ value?

  1. $y=(\text{int})(x+0.5)$
  2. $y=\text{int} (x+0.5)$
  3. $y=(\text{int}) x+0.5$
  4. $y=(\text{int})(\text{int})x+0.5)$
recategorized by

3 Answers

4 votes
4 votes

Here the correct way to round of the float values are :

whenever, a decimal value of x <5 the ans would be the floor value of x .

and when  decimal value of x >5 the ans would be the ceil value of x.

for eg.

if x=5.75 , the correct round off value will be 6. i.e y=6.:

so here (int)(5.75+0.5) = (int) (6.25) = 6.

 

and if x=5.25 ,the correct round off value will be 5. i.e y=5.:

so here (int)(5.25+0.5) = (int) (5.75) = 5

 

so adding 0.5 in x and returning floor value  will return the correct round off value . which is equivalent to OPTION "A" i.e (int )(x+0.5)   Here Type casting int means give the floor value of x+0.5 which is what we want in the answer 

hence Option "A" is correct. 

 

0 votes
0 votes
I think option B is correct.

Let us suppose x = 2.75

Now y= int ( x +0.5) = int (2.75+0.5)= int (3.25)= 3
0 votes
0 votes
In the question.

Given ::   x = float

Say p = int

To round off “float” variable  to “int” variable in C.   

p = (int)x

Hence according to question option C) seems to be more suitable.
Answer:

Related questions

5 votes
5 votes
6 answers
1
admin asked Mar 31, 2020
2,098 views
What error would the following function give on compilation? f(int a, int b) { int a; a=20; return a; }Missing parenthesis is $\textit{return}$ statement.Function should ...
4 votes
4 votes
1 answer
2
admin asked Mar 31, 2020
2,034 views
Prior to using a pointer variable it should bedeclared.initialized.both declared and initialized.none of these.
4 votes
4 votes
1 answer
3
admin asked Mar 31, 2020
3,242 views
Output of the following loop isfor(putchar('c');putchar ('a');putchar('r')) putchar('t');a syntax error.cartrt.catrat.catratratratrat...
2 votes
2 votes
1 answer
4
admin asked Mar 31, 2020
1,123 views
If space occupied by two strings $s_1$ and $s_2$ in 'C' are respectively $m$ and $n$, then space occupied by string obtained by concatenating $s_1$ and $s_2$ is alwaysles...