0

Need help with C challenge

You go trick or treating with a friend and all but three of the houses that you visit are giving out candy. One house that you visit is giving out toothbrushes and two houses are giving out dollar bills. Task Given the input of the total number of houses that you visited, what is the percentage chance that one random item pulled from your bag is a dollar bill? Input Format An integer (>=3) representing the total number of houses that you visited. Output Format A percentage value rounded up to the nearest whole number. Sample Input 4 Sample Output 50 For me, the hidden test cases are failing and I don't know why https://code.sololearn.com/cFan4RT8MK2C/?ref=app

14th May 2021, 3:04 AM
Rishi
Rishi - avatar
4 odpowiedzi
+ 4
Output should be rounded up to the nearest integer. So you need to use ceil() instead of round () printf("%i",(int)ceil(((double)200/(double)houses)))
14th May 2021, 3:36 AM
Simba
Simba - avatar
+ 1
Simba yeah it worked, thank you. Why doesn't "round" work here? If the number is 50.1, ceil function rounds it to 51, but it really has to be rounded to 50 right? Then how does ceil work here?
14th May 2021, 10:01 AM
Rishi
Rishi - avatar
0
Because the definition for ceil() is that it returns ceiling value of x i.e., the smallest integer not less than x. That's why 50.1 is rounded up to 51 not 50 (50 < 50.1 < 51)
14th May 2021, 12:08 PM
Simba
Simba - avatar
0
Simba yeah I know that. But my doubt is that, isn't the value 50.1 rounded to 50 instread of 51 in this question?
16th May 2021, 3:11 PM
Rishi
Rishi - avatar