0

Can anyone explain why I am getting output as 12 ?

public class Program { public static void main(String[] args) { int var1=5; int var2=6; int var3; var3=++var2*var1/var2+var2; System.out.println(var3); } }

16th May 2021, 3:26 PM
ajay singh
ajay singh - avatar
3 Answers
+ 1
++Var2=7*var1=5/var1=7 =7*5/7=5 Now var2=7+5=12 You will get the answer 2 if you enclose each operation in common brackets or parenthesis
16th May 2021, 3:41 PM
Atul [Inactive]
+ 1
According to BODMAS we should do var1/var2?
16th May 2021, 3:45 PM
ajay singh
ajay singh - avatar
+ 1
Var3=++6*5/6+6 Var3=7*5/7+7 Var3=35/7+7 Var3=5+7 Var3=12
16th May 2021, 3:49 PM
Eze