+ 4

Halloween candy

#include <iostream> using namespace std; int main() { int houses; cin>>houses; //your code goes here float probab=2.0/houses; int res = probab*100; if(probab<=0.5) cout<<res<<endl; else cout<<++res<<endl; return 0; } Can anyone tell me ...what's wrong with this and why I passed 2 cases but failed to pass the other 3!!

7th May 2020, 9:14 AM
Andrina
11 Answers
+ 5
Vaibhav Tyagi Condition is houses should be greater that or equal to 3. So if(houses >= 3) { res = 2.0 * 100 / houses; } res = ceil(res); cout << (int) res;
7th May 2020, 9:21 AM
A͢J
A͢J - avatar
+ 7
AJ #Infinity Love voila ...it worked !! Thankyou
7th May 2020, 9:28 AM
Andrina
+ 3
Yeah I am getting...can you tell me what are the other three cases ! ...is this logic wrong to get the nearest rounded value??
7th May 2020, 9:17 AM
Andrina
7th May 2020, 9:18 AM
Andrina
+ 3
AJ #Infinity Love ohk ...I will try this !
7th May 2020, 9:22 AM
Andrina
+ 3
Rithea Sreng the logic of if(probab<=0.5) is because Probab is always 0-1 in this program ! So if I explicitly type convert it then it'll always drop the decimal part and return the lower value of int ...which is acceptable when the decimal part is less then 0.5 but when the decimal part is greater than 0.5 it is to incremented by one. But if you are saying that ...it is always have to be rounded up ...then you are right! 😅
7th May 2020, 4:08 PM
Andrina
+ 2
Vaibhav Tyagi We can't see other cases.
7th May 2020, 9:18 AM
A͢J
A͢J - avatar
+ 2
John Egan I didn't get you !! Is there a problem ?
8th May 2020, 8:54 AM
Andrina
+ 1
Vaibhav Tyagi You should get nearest whole number means rounded value. res = ceil(res); cout << (int) res;
7th May 2020, 9:16 AM
A͢J
A͢J - avatar
0
Wt voogle u guy,s gal,s texting about hummm i gotta learn yup...
8th May 2020, 6:44 AM
John Egan
John Egan - avatar
0
#include <iostream> using namespace std; int main() { int houses; cin>>houses; float res; if(houses >= 3) { res = 2.0 * 100 / houses; } res = res+ 0.99; cout << (int) res;}
16th Nov 2020, 4:37 AM
Akash Palla
Akash Palla - avatar