+ 1
why is the output of this code is 1.0 ,whereas i'm getting an answer of 9?
a = 6 b = 3 a /= 2 * b print(a) correct ans: 1.0 My ans : 9
13 Réponses
+ 6
sai a /= 2 * b is the same as:
a = a / (2 * b)
+ 3
How did you get 9?
+ 3
Oma Falk answer is 1 and only 1.
a/=2*b implies 2*b calculated at first and then we perform division.
a/=2*b
=> a=a/(2*b) and no other stuff is legal/valid
+ 3
sai according to your code this will be the only correct answer
The correct answer is 1 as a/= 2*b is same as a/(2*b) = 6/(2*3) = 6/(6) = 1
Hence Proved
Use bodmas to calculate this.
Always use brackets and correct syntax to code.
Hope you had understood
+ 3
Code to get 9
//write the code
a+= b
//print a with the new value
print(a)
+ 3
a/=2*b is equivalent to a=a/(2*b)
Therefore,
a=6/(2*3)
=6/6
=1.0
+ 2
Jishnu Mukherjee that are true words.
.. well 1.0...
I simply debugged the thoughts of sai to understand what made him think it was 9.
+ 2
2*b must be in ()
+ 1
it is the priority of operator.
* is higher than /=
and therefore ti. e performed first.
+ 1
@oma faik but what about associativity as both the * and / have same precedence LTR associativity is used,that is what confusing me
+ 1
Jishnu Mukherjee
😱😱😱😱😉😉
ups
thanks for debugging
+ 1
Oma Falk I did jackshit.
first we compute 2*b which is 6 and then divide by a whose value is 6.
so 6/6=1
+ 1
sai right
but our operators are
/=
and
*
and now it is clear for me why you asked.
you saw
/
and
*
and... Jishnu Mukherjee
a/2 =3
3*3 = 9
lets write it different
a= a /(2*b)