+ 1
Please, what is the output of this code? Show the steps used in solving it.
int x =3/2*4+3/8+3; cout<<x;
4 odpowiedzi
+ 3
/ and * have priority so here is how you solve it:
3/2=1.5 but because both numbers are integer the result will saved also as integer, so it save 1
1*4=4
3/8=0.375 but are integer again so save 0
then you add the numbers you calculated:
4+0=4
4+3=7
so result is 7, if do the calculation with a calculator the result is different because calculator using floating numbers.
If want have the same result as a calculator you need to do 2 changes:
1) change the type of x from int to float.
2) make (at least) the 1 of the 2 numbers from the first calculation (3/2) float (for example 3.0f/2)