+ 3

Why is 8 the answer java

//Java question static int value = 8; static void click (){ if (value%4=0; value/=2; else value+=3; } public static void main(String[] args) { for(int i=0; i <=3; i++) click(); System.out.print(value); } Thanks..

31st Aug 2020, 7:16 PM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar
2 Answers
+ 5
The for loop will run 4 times First loop=> The if statement will be followed! (Since 8%4 == 0) 8/2 = 4 value = 4 Second loop => The if statement will be followed! (Since 4%4 == 0) 4/2 = 2 value = 2 Third loop => The else statement will be followed! (Since 2%4 != 0) 2+3 = 5 value = 5 Fourth loop => The else statement will be followed! (Since 5%4 != 0) 5+3 = 8 value = 8 Hence, the value again becomes 8 🙌
31st Aug 2020, 7:21 PM
Namit Jain
Namit Jain - avatar
+ 1
i= 0; => click() ; 8%4==0 true, then => value=value/2=8/2=4 i =1, click() 4%4==0 => value =4/2=2 i=2, =>click() 2%4==0 false then, in else value+=3=>vlaue=2+3=5 i=3 =>click() 5%4==0, false, then in else value= 5+3= 8. I=4, for loop condition false, so it exits.. vlaue =8 now, so output is 8.
31st Aug 2020, 7:28 PM
Jayakrishna 🇮🇳