look at my code:
int main()
{
int a;
a=(5/9)*2;
cout<<a;
return 0;
}
here it would print 0. if i change 9 to 9.0 , still it prints 0. But if i change both 5 and 9 to 5.0 and 9.0 then it prints 1.
why?
look at my code:
int main()
{
int a;
a=(5/9)*2;
cout<<a;
return 0;
}
here it would print 0. if i change 9 to 9.0 , still it prints 0. But if i change both 5 and 9 to 5.0 and 9.0 then it prints 1.
why?
this is because of implicit type conversion
and also because you are storing float value in a int bucket
i have explained all combination in code
first you look at code and read the explanation and then run it and check the output with your understanding
Example Code
#include <iostream>
using namespace std;
int main() {
cout << (5 / 9.0) << endl;
/// here we are doing int/float == float hence ouput should be float=0.5555
int a;
a = (5 / 9.0);
cout << a << endl;
a = (5.0 / 9.0);
cout << a << endl;
// in both the cases above we have the same thing but the ans which is float we are storing it in int
// hence 0.5555 reduced to 0
a = (5 / 9.0) * 2;
cout << endl;
a = (5.0 / 9.0) * 2;
cout << endl;
/// in these above 2 case we are mutiplying the 0.555 with 2 result into 1. somthing which
// when stored in int result into 1
float b;
b = (5 / 0.9);
cout << b << endl;
// this is the correct way to store float nos in float bucket
}
if you want to ask something about this feel free to ask
i hope this helps
if yes show your response with
and don’t forgot to mark doubt as resolved