0

What am I doing wrong. It’s about Halloween candy practice.

#include <iostream> #include <cmath> using namespace std; int main() { int houses; int dollar = 2; int chances; cin>>houses; chances = dollar*100/houses; cout << chances; return 0; }

24th Jul 2022, 9:49 AM
Cojo Alex
Cojo Alex - avatar
5 ответов
+ 4
You could use the % operator to check if the result is a decimal. If it is a decimal, you round up to the next integer. round up & round are 2 different meanings. #include <iostream> #include <cmath> using namespace std; int main() { int houses; int dollar = 2; int chances; cin>>houses; chances = dollar*100/houses; if((dollar*100) %houses==0){ cout << chances; } else { cout << chances +1; } return 0; }
24th Jul 2022, 10:40 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
You have to roundup the value see input and output format
24th Jul 2022, 9:54 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
ceil(dollar*100.0/houses)
24th Jul 2022, 10:40 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Thanks a lot. I understood both methods.
24th Jul 2022, 12:14 PM
Cojo Alex
Cojo Alex - avatar
0
Still not working. I get the same error. #include <iostream> #include <cmath> using namespace std; int main() { int houses; int dollar = 2; double chances; int result; cin>>houses; chances = dollar*100/houses; result = round(chances); cout << result; return 0; }
24th Jul 2022, 10:04 AM
Cojo Alex
Cojo Alex - avatar