Why the output of this code is 2 ????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the output of this code is 2 ?????

Predict the output of following using operator precedence:- 5-2*3+3

15th Dec 2016, 1:32 AM
amit shankar
amit shankar - avatar
4 Answers
+ 8
Do you remember learning about PEMDAS in school? It applies similarly to programs, but not exactly. Parentheses are done first, multiplication AND division are done left-to-right, and addition AND subtraction are done left-to-right. So in this example, you'd do 2 * 3 first, then 5 - 6, then -1 + 3.
15th Dec 2016, 2:02 AM
Tamra
Tamra - avatar
+ 1
Apply BODMAS rule and precedence of operators in C language... *,/ are of same precedence and +,- are of same precedence ...(*,/) > (+,-)...and in tie cases, it follows left to right approach. As we have only one higher precedence operator i.e., *... 2*3=6 will be performed first...5-6=-1 performed second and lastly -1+3=2 ...hence the answer:2
15th Dec 2016, 8:27 AM
Manikanta Nallamalli (Mittu)
Manikanta Nallamalli (Mittu) - avatar
+ 1
multiplication gets 1st priority so, -2*3=-6 then addition so -6+3=-3 then subtraction so 5-3=2 .
16th Dec 2016, 5:27 PM
CougAr
CougAr - avatar
0
yeah! i got it thanks all..
15th Dec 2016, 1:02 PM
amit shankar
amit shankar - avatar