What is the output of this code and why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 16

What is the output of this code and why?

int x = 4; int y = ++x * 4 / x + x; System.out.print(y + x); Please anyone help me to shortout the problem. Thanks in advance!!!☺

25th Aug 2017, 7:51 AM
Tanjibur Rahman
Tanjibur Rahman - avatar
65 Answers
+ 36
++x=5 //incrementation of x by 1, x=x+1=>4+1=5 now, x=5 //x becomes 5 due to incrementation ++x*4/x=>5*4/5=4 y=++x*4/x+x =4+5 =9 Now, y=9 and x=5 So, output will be 9+5= 14 The incremented value of x is used in place of x... So output you'll get is 14...
26th Aug 2017, 4:35 AM
Ankit Saxena
Ankit Saxena - avatar
+ 25
According to diagnostics, the value of y is 9. The prefix increment statement is evaluated prior to statement evaluation, hence all x in the y assignment statement is 5. y = 5 * 4 / 5 + 5 y = 9 Final output prints (x + y), which is (5 + 9) = 14.
25th Aug 2017, 8:02 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
@Tanjibur Because ++x increments x by 1 and store it back to x.
25th Aug 2017, 8:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
++x increments value by 1 and uses it. x++ uses it and increments value by 1.
25th Aug 2017, 8:09 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 8
@vengat ... if this continued then it will become question of the day 😂😂😂
26th Aug 2017, 7:18 AM
Changed
Changed - avatar
+ 7
@Tanjibur I made a careless mistake, using the non ++ed value of x. Answer now edited. Thanks for pointing it out
25th Aug 2017, 8:05 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
You made me do my first java code too! TBH I suck at Java
25th Aug 2017, 8:07 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
sounds like your homework hahahaa int x=4 int y=(++x=5)*4/(5)+5 y=9 x+y=5+9=14 Why not you try on code playground?
25th Aug 2017, 7:58 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
25th Aug 2017, 8:02 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
@Tanjibur Rahman ++ is an increment operator, it increments a value by 1. The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The only difference is that the prefix version (++x) evaluates to the incremented value, whereas the postfix version (x++) evaluates to the original value.
25th Aug 2017, 8:14 AM
Daniel Stanciu
Daniel Stanciu - avatar
+ 5
My advice would be to do some reading on operator precedence in Java (read the official Oracle documentation and play with code). Operators with higher precedence are evaluated before operators with relatively lower precedence. The unary (prefix) operator has higher precedence than multiplicative operators, which in turn have higher precedence than additive operators. That means ++x gets evaluated first. So y = 5 * 4 / 5 + 5 x is now 5 and y is 9 and your code outputs 14 ☺️
25th Aug 2017, 8:15 AM
Daniel Stanciu
Daniel Stanciu - avatar
+ 5
++x just works like that. Can't say anything more. Javascript PHP, C++,C# all use these incrementers
25th Aug 2017, 8:16 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
Please people I would like to end this discussion. Answer is settled. So is explanation. ----------CLOSE THIS QUESTION-------------
26th Aug 2017, 7:14 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
u got y=5 *4/5 +5 ☝☝☝ what will u do here( 5*4 then divide by 5 ) = 4 or (5 * 4 divide by 5) =0 this is ur main doubt , then buddy just see 👉precedence order👈 for doing these question
25th Aug 2017, 8:13 AM
Changed
Changed - avatar
+ 4
Here ++ used before x is a prefix increment operator. It first increments the value of x by 1and then further calculation goes on. Therefore:- y = 5*4/5+5; (y= 4+5=9) Now x+y = 9+5 = 14
25th Aug 2017, 9:14 PM
Suyash
+ 4
ans is also 14. ++x++4=5 y=9 x+y=5+9=14 x is increment
27th Aug 2017, 2:49 AM
ishadi Rathnayake
ishadi Rathnayake - avatar
+ 4
guys guys guys he knows the solution now no need to answer
27th Aug 2017, 9:32 AM
Changed
Changed - avatar
+ 4
output is 14.... points that u need to remember is that 1)value gets incremented from right to left 2)During arithmetic operations Priority order of the application operators follow the rule of BODMAS 3)And of course when u are addin' up x & y for the output don't forget tp take the incremented valure of x which is 5
27th Aug 2017, 7:04 PM
Yash Raghava
Yash Raghava - avatar
+ 3
Tnxx Daniel Stanciu ☺ Now I get it
25th Aug 2017, 8:15 AM
Tanjibur Rahman
Tanjibur Rahman - avatar
+ 3
I got the result as 14 according to my calculation, and i think lot many explanations are already given before i saw the question.
25th Aug 2017, 2:58 PM
Yagnavalk Chaapala
Yagnavalk Chaapala - avatar