0

Halloween Candy cpp

Why the output is 0 (to any input)? #include <iostream> using namespace std; int main() { int houses; cin>>houses; //your code goes here int res; res=2/houses*100; cout<<res<<endl; return 0; }

29th Jul 2022, 12:21 PM
samuraicat
2 Answers
+ 4
Because if you use houses of type int, the number will be rounded. For example, let s say your input is 5. 2/5 will give you 0 because the number will be rounded. Try the data types "double" or "float" for the input and res.
29th Jul 2022, 12:28 PM
šŸ‡ Alex Tușinean šŸ’œ
šŸ‡ Alex Tușinean šŸ’œ - avatar
- 1
šŸ‡ Alex Tușinean šŸ’œcan you please explain why is it correct: #include <iostream> #include <cmath> using namespace std; int main() { float houses; cin>>houses; //your code goes here float res; res=2/houses*100; cout<<ceil(res)<<endl; return 0; } while the same code with int houses is not? Am I not supposed to use integers as a denominator when the result has a float/double type or what?
29th Jul 2022, 1:11 PM
samuraicat