What's the fault in paint cost? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What's the fault in paint cost?

One of five test get wrong! https://code.sololearn.com/c8Sybq6Z7iVx/?ref=app

22nd Jan 2020, 4:11 AM
Anand Singh
Anand Singh - avatar
8 Answers
+ 12
In your code you're not rounding a value of float to it's nearest value. You just setting decimal places. #include <stdio.h> int main(){ float a = 3.14; printf("%.1f" a); // Prints one decimal place. // Output: 3.1 return 0; } To round up to the nearest value. Use ceil() function (defined in header file math.h) #include <stdio.h> #include <math.h> int main(){ float a = 3.9; int b; b = ceil(a); // returns integer value rounded up to the nearest value. printf("%d", b); // Output: 4 return 0; }
22nd Jan 2020, 5:09 AM
Vinay_GB
Vinay_GB - avatar
+ 5
Thanks for your response diego Code! Now I got to know about rounding through printf() built-in function. 😉 printf("%.1f", 3.16); // 3.2 So, if value of 2nd decimal place is greater than 5, it will be rounded up to its nearest integer. printf("%.1f", 3.15); // 3.1 Since the 2nd decimal place of this float number is equal to 5, it won't be rounded up to its nearest integer.
22nd Jan 2020, 1:01 PM
Vinay_GB
Vinay_GB - avatar
+ 4
Read the task carefully. It says to round the result.
22nd Jan 2020, 4:30 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Vinay_GB / Anand Singh printf() does some rounding... BUT in some cases...well ... Things get "weird" printf("%.0f\n",82.5); // 82 :) printf("%.0f\n",83.5); // 84 (The rounding is implementation deoendent)
22nd Jan 2020, 12:41 PM
unChabon
unChabon - avatar
+ 2
Use round() Instead of %.0f I.e. printf("%d", (int)round(p * 1.1));
22nd Jan 2020, 5:23 AM
unChabon
unChabon - avatar
+ 2
Vinay_GB you wish!! Haha Try: printf("%.1f %.1f\n", 3.15, 4.15 ); That's why I said Use round() :) https://code.sololearn.com/cc0ril9k2HSG/?ref=app
22nd Jan 2020, 1:13 PM
unChabon
unChabon - avatar
0
therefore I used "%.0f" for round off to nearest whole number
22nd Jan 2020, 4:33 AM
Anand Singh
Anand Singh - avatar
0
#include <stdio.h> #include<math.h> int main() { int canvas,colour ,value; float cost,tax=0.1; scanf("%d",&colour ); canvas=40; cost =(canvas )+((canvas)*0.1)+(colour *5)+((colour*5)*0.1); //ceil(cost); value =ceil(cost); printf ("%d",value); return 0; }
23rd Jan 2020, 1:24 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar