+ 2

[Solved by jayakrishna]Code coach paint cost problem in C

What's wrong with my code? It can't pass case3 and case4. #include <stdio.h> int main() { int a; scanf("%d",&a); a*=5; a+=40; int b; if(a%10==0){ b=a/10; printf("%d",a+b); } else if(a%10!=0){ float c; c=a/10; printf("%.2f",a+c); } return 0; }

23rd Feb 2020, 12:10 PM
高于鈞
高于鈞 - avatar
10 ответов
+ 2
cout<<(int)ceil(c); for c, int c=ceil(a+b);
23rd Feb 2020, 2:50 PM
Jayakrishna 🇮🇳
+ 2
Try by taking 'a' as double or float. Output should rounded up, converted to int value only. I guess the problem is in else part only.. should be %d not %f...
23rd Feb 2020, 12:22 PM
Jayakrishna 🇮🇳
+ 2
Input is number of colours, it's not something like half blue or half yellow exist right?but output did, output is price+price÷10. I still don't get it... pls help..
23rd Feb 2020, 2:05 PM
高于鈞
高于鈞 - avatar
+ 2
No. I mean if it is int, then a/10 always return int values only.... So you need, a double value to get fraction part also.. Either take 'a' as double or do like a/10.0....
23rd Feb 2020, 2:10 PM
Jayakrishna 🇮🇳
+ 2
I edit code like this it still failed in case 3 and 4. #include <stdio.h> int main() { float a; scanf("%f",&a); a*=5; a+=40; float b; b=a/10; int c=a+b; printf("%d",c); return 0; }
23rd Feb 2020, 2:22 PM
高于鈞
高于鈞 - avatar
+ 2
a+b is should be rounded up, so use ceil function to result of a+b, and store in c...
23rd Feb 2020, 2:28 PM
Jayakrishna 🇮🇳
+ 2
Thanks!!! The ceil function was working successfully! #include <stdio.h> int main() { float a; scanf("%f",&a); a*=5; a+=40; float b; b=a/10; float c=a+b; printf("%.0f",ceil(c)); return 0; }
23rd Feb 2020, 2:35 PM
高于鈞
高于鈞 - avatar
+ 2
But my cpp code failed in case 3 and 5... #include <iostream> #include <cmath> using namespace std; int main() { float a; cin>>a; a*=5; a+=40; float b; b=a/10; float c=a+b,x; x=ceil(c); cout<<x; return 0; }
23rd Feb 2020, 2:44 PM
高于鈞
高于鈞 - avatar
+ 2
It's work! Thanks you very much!
23rd Feb 2020, 2:52 PM
高于鈞
高于鈞 - avatar
+ 1
高于鈞 Wel come..
23rd Feb 2020, 2:53 PM
Jayakrishna 🇮🇳