C Programming - Floating Point Issues - Discussion

Discussion :: Floating Point Issues - Find Output of Program (Q.No.8)

8. 

What will be the output of the program?

#include<stdio.h>
#include<math.h>
int main()
{
    float n=1.54;
    printf("%f, %f\n", ceil(n), floor(n));
    return 0;
}

[A]. 2.000000, 1.000000
[B]. 1.500000, 1.500000
[C]. 1.550000, 2.000000
[D]. 1.000000, 2.000000

Answer: Option A

Explanation:

ceil(x) round up the given value. It finds the smallest integer not < x.
floor(x) round down the given value. It finds the smallest integer not > x.

printf("%f, %f\n", ceil(n), floor(n)); In this line ceil(1.54) round up the 1.54 to 2 and floor(1.54) round down the 1.54 to 1.

In the printf("%f, %f\n", ceil(n), floor(n)); statement, the format specifier "%f %f" tells output to be float value. Hence it prints 2.000000 and 1.000000.


SaireddySareddy said: (Sep 20, 2013)  
It is like our ceiling and flooring.

Ceiling means up (up value).

Flooring means down (down value).

Nakulrai said: (May 30, 2017)  
But why we have to round off? Explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.