C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 5)
5.
How would you round off a value from 1.66 to 2.0?
ceil(1.66)
floor(1.66)
roundup(1.66)
roundto(1.66)
Answer: Option
Explanation:
/* Example for ceil() and floor() functions: */

#include<stdio.h>
#include<math.h>

int main()
{
    printf("\n Result : %f" , ceil(1.44) );
    printf("\n Result : %f" , ceil(1.66) );
 
    printf("\n Result : %f" , floor(1.44) );    
    printf("\n Result : %f" , floor(1.66) );

    return 0;
}
// Output:
// Result : 2.000000
// Result : 2.000000
// Result : 1.000000
// Result : 1.000000
Discussion:
79 comments Page 1 of 8.

Elumalai said:   1 decade ago
Floor : It is used to eliminate the float(decimal)numbers that means if the value is 3.50 means the result is 3.00. It eliminate the .50.

Ceil : It is used to round the next integer value that means if the value is 3.50 or 3.4 the result is 4.

Round : It is used to round up or round down the float numbers based on the numbers. If the value is <3.5 then the result is 3 and >= 3.5 then v will get 4.

Anil Chauhan said:   10 years ago
#include <stdio.h>
#include <math.h>

int main ()
{
float val1, val2, val3, val4;

val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;

printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));

return(0);
}

S.Sasikumar said:   1 decade ago
ceil()-This function works as follows: if the value is greater than or equal to (_.5) then, it round off a value into the next integer value. Otherwise it round off into before integer value of the given float number. Floor()-This function works as follows: if the value is floating point number then it eliminates the float values and gives only integer value before the given float number.

Akshatha said:   1 decade ago
@Sasikumar.

You said that ceil() - This function works as follows: if the value is greater than or equal to (_.5) then, it round off a value into the next integer value.

Otherwise it round off into before integer value of the given float number, but in the explanation part they have given an example in that the o/p of ceil(1.44) is 2 how can it be? it was suppose to b 1 right?

RAJ said:   10 years ago
Why the output is 0 instead of having integer value 2?

#include<stdio.h>
#include<math.h>

int main()
{
printf("\n Result : %d" , ceil(1.44) );
printf("\n Result : %d" , ceil(1.66) );

printf("\n Result : %d" , floor(1.44) );
printf("\n Result : %d" , floor(1.66) );

return 0;

Lokesh said:   1 decade ago
Guys ceil() function deletes the fractional value and increments the number by 1, whereas floor() function deletes the fractional part and displays the number. Here in these two functions the fractional part is not considered.

Eg:
ceil(1.66) = 2
ceil(1.10) = 2
floor(1.90)= 1
floor(1.33) = 1

Jayasri said:   1 decade ago
roundup() function preforms just rounding the value 10.5 to 11
and roundto() function performs like specification if u want to display 2 decimal r three decimal. its like 123.345 u want it for 0 decimal means it displays 123. and for 1 its displays 123.3 like wise it performs

Chandan said:   1 decade ago
I take daily life exa.
Ceil mean ceiling fan i.e. Upper limit
Floor mean ground i.e. Lower limit
Here I take upper limit mean +positive infinity n lower is -neg
Infinity

During ceil function we round value toward positive value

i.e. 3.2=4
3.6=4
2.1=3

Shankar said:   1 decade ago
#include<stdio.h>
int main()
{

int x;
x=ceil(1.2) ;
printf("%f ",x);
printf("%f",ceil(1.2));
return 0;
}

Difference b/w two statement in this program.

1. x=ceil(1.2) ;
2. printf("%f",ceil(1.2));

Binit said:   1 decade ago
ceil() - roundup a float no to its upper integer value
ex : ceil(1.9) = 2.0000
ceil(1.1) = 2.0000

floor() - roundup a float no to its lower integer value

ex : floor(1.9) = 1.0000
floor(1.1) = 1.0000


Post your comments here:

Your comments will be displayed after verification.