I have this code in c++:
#include <iostream>
using namespace std;
int main() {
float division = 5/2;
cout<<division;
return 0;
}
But it return 2
, instead of 2.5
, why ?
I have this code in c++:
#include <iostream>
using namespace std;
int main() {
float division = 5/2;
cout<<division;
return 0;
}
But it return 2
, instead of 2.5
, why ?
Because you are dividing int
egers, not float
s. What you assign the result to is irrelevant to how the division is performed.
If you want to divide floats, do
float division = 5.f/2.f;