+ 2

What's wrong with the 'C lang' code?

QUESTION: https://www.sololearn.com/coach/22?ref=app MY  SOLUTION IN PYTHON: color = int(input()) price =  color  + 40.00 salesTax =  0.1 + 1 total = price * salesTax print(round(total)) #works very  fine MY  SOLUTION IN C: #include <stdio.h> #include <math.h> int main() {     int colorNo;     scanf("d", &colorNo);         float color = colorNo * 5.00;     float price = color + 40.00;     float salesTax = 0.1 + 1.0;         float total = price * salesTax;     printf("%d", ceil(total));         return 0; } /* outputs 1 on every test, so what am i doing wrong? */

27th Aug 2021, 4:33 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
2 Answers
+ 7
1. Missing % before conversion specifier in first scanf(). 2. ceil() returns a floating-point value, you should cast that to an integer before printing it with %d, otherwise it will be interpreted in a wrong way. 3. The code you posted was littered with unknown whitespace-like characters the compiler didn't recognize. This cleaned-up version should work: #include <math.h> #include <stdio.h> int main() { int colorNo; scanf("%d", &colorNo); float color = colorNo * 5.00; float price = color + 40.00; float salesTax = 0.1 + 1.0; float total = price * salesTax; printf("%d", ( int )ceil(total)); return 0; }
27th Aug 2021, 4:45 PM
Shadow
Shadow - avatar
+ 4
You might also be wondering why it outputs 1 when you mismatched the format string and its parameter, in which case this can be a beneficial read: https://stackoverflow.com/questions/7480097/what-happens-to-a-float-variable-when-d-is-used-in-a-printf
27th Aug 2021, 4:51 PM
Hatsy Rei
Hatsy Rei - avatar