C Programming - Library Functions - Discussion
Discussion Forum : Library Functions - Find Output of Program (Q.No. 2)
2.
What will be the output of the program?
#include<stdio.h>
#include<math.h>
int main()
{
float i = 2.5;
printf("%f, %d", floor(i), ceil(i));
return 0;
}
Answer: Option
Explanation:
Both ceil() and floor() return the integer found as a double.
floor(2.5) returns the largest integral value(round down) that is not greater than 2.5. So output is 2.000000.
ceil(2.5) returns 3, while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.
Discussion:
18 comments Page 2 of 2.
Anusha said:
1 decade ago
Can anyone please explain how it becomes 0 ?
PRIYANKA said:
1 decade ago
Please explain the conversion of double to integer.
Abc said:
1 decade ago
Can anyone explain double to int conversion.
Chandana said:
1 decade ago
Conversion is confusing please explain the conversion of double to integer.
Swathi said:
1 decade ago
Can anyone please explain me the procedure of double to int conversion ?
Prakash said:
1 decade ago
ceil(2.5)it returns 3,
while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.
while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.
Archana said:
1 decade ago
Please give me clarity how it became 0 ans may be 2, 3 also but you gave those values why?
Sundar said:
1 decade ago
@Anusha
Use the casting operator (int) along with ceil(). This will convert the 'double' value to 'int' value.
printf("%f, %d", floor(i), (int)ceil(i));
The output will be : 2.000000, 3
Use the casting operator (int) along with ceil(). This will convert the 'double' value to 'int' value.
printf("%f, %d", floor(i), (int)ceil(i));
The output will be : 2.000000, 3
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers