+ 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? */
2 Respuestas
+ 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;
}
+ 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