+ 4

What is the output of these code?And how?

int a= 2; int b= 3; for(int i=3; i >1; i--) { a+=b/i; } System.out.print(a);

28th Apr 2017, 5:41 AM
Hasrat Wadsariya
Hasrat Wadsariya - avatar
5 odpowiedzi
+ 8
try this :) import java.util.*; class Q12{ public static void main(String[] args){ int a= 2; int b= 3; for(int i=3; i >1; i--) { a+=b/i; } System.out.print(a); } } .............. yu will get 4 !
28th Apr 2017, 6:12 AM
‏‪‏‪‏‪‏‪‏‪Lamiya‬‏‬‏‬‏‬‏‬‏
‏‪‏‪‏‪‏‪‏‪Lamiya‬‏‬‏‬‏‬‏‬‏ - avatar
+ 4
*loop runs 2 times, a=2 and b=3. *1st iteration, a=a+b/i; a=2+3/3; \\a=2+1 so a=3. *2nd iteration a=3+2/2; \\since(i--) i=2 so a=4; *condition fails(i>1) i=1 finally comes out of loop and prints 4;
28th Apr 2017, 7:21 AM
Manideep
Manideep - avatar
+ 3
is that the letter l in the line for(int i=3; I>1; i--) { if that is the case you get an error
28th Apr 2017, 5:47 AM
chris
chris - avatar
+ 3
chris that is i > 3
28th Apr 2017, 6:08 AM
Nadir wadsariya
Nadir wadsariya - avatar
+ 3
Nadir wadsariya the question was updated that is why I wrote that
28th Apr 2017, 6:14 AM
chris
chris - avatar