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; }
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.
- 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?