+ 1

[C] Hallowe'en Candy

I can output a rounded up figure and I include user input validation. But what else am I missing here? #include <stdio.h> #include <math.h> /* function declaration */ double calculate(double); int main() { double houses; // variable double percent; do { // prompt user for input scanf("%lf", &houses); } while (houses < 3); // continue prompting if input is < 3 percent = calculate(houses); // assign variable to function value printf("%.0lf\n", round(percent)); // print value and round it } /* function for calculation */ double calculate(double input) { double result; double dollar = 2.0; result = dollar / input; result = result * 100.0; return result; }

19th Oct 2020, 4:43 PM
Kaila
3 odpowiedzi
+ 2
I remember that I lost a lot of time with this challenge for the same reason! I was rounding the number to the nearest integer, but the problem wants you to round to the smallest integer greater than or equal to the number. For example if the value of percent is 5.3 the program should print 6 and not 5. You can do it with the function ceil() Anyway I checked my solution and since it is really short I wanted to show it to you: #include <stdio.h> #include <math.h> int main() { int houses; scanf("%d", &houses); printf ("%d", (int) ceil(200.0/houses) ); return 0; } The (int) cast operator is there because ceil return a double, but I want to print an int. Also you can divide a floating point number for an integer cause in that case the program will automatically create a floating point version of the int and do the math.
19th Oct 2020, 8:41 PM
Davide
Davide - avatar
+ 2
Kaila You are welcome!
19th Oct 2020, 11:36 PM
Davide
Davide - avatar
+ 1
Thank you so much Davide for your response. I really appreciate you showing me your version; a lot shorter than mine!
19th Oct 2020, 11:32 PM
Kaila