0
I have problem with Holloween Candy challenge for c programming language. Can anyone help me?
This is my code: #include <stdio.h> #include <math.h> int main() { int houses; float dollar; scanf("%d", &houses); //your code goes here dollar = (float)200/houses; printf ("%.0f",dollar) ; return 0; } I just put 200 because it is shortcut of (2*100) as two represent the number of dollar and 100 to find percentage value. I already passes test case 1 and 2 but the rest, i don't know which part was wrong since its hidden test case.
6 Respostas
+ 2
The result has to be rounded up.
#include <math.h>
dollar = ceil(200.0 / houses);
+ 2
ceil() returns a double so you'll need to cast it to an int
(int)ceil()
https://www.tutorialspoint.com/c_standard_library/c_function_ceil.htm
+ 1
Its just by the definition how the output should be:
Output Format
A percentage value rounded up to the nearest whole number.
So e.g. 12.1 should not be printed as 12, but 13.
0
Thank you for those who answer my question, finally i get it. However, why we can't use my method? Any further explanation?
0
Ouhhh, i thought it should be round up like what we have learnt in school which is if 12.1 until 12.4 will be printed as 12 and if 12.5 until 12.9 will be printed as 13. Thank you for your very helpful and useful information 😊.
0
Radin Misbah
When the problem description asks you to round up, it means round up to the nearest higher integer. Therefore you need to use the ceil function.
If it were to say round down then you need to round down to the nearest lower integer. Then you need to use the floor function.
To round it to the nearest integer as you describe you can use the round function.