0

Can anyone explain this code please :)

public class Program { public static void main(String[] args) { int num=4162; int temp=0; while(num>0) { temp = temp +num%10; num /= 10; } System.out.println(temp); } } Output:13

9th Jul 2020, 10:05 PM
Anirudhan R
Anirudhan R - avatar
1 ответ
+ 3
temp=0+2 num=416 temp=2+6 num=41 temp=8+1 num=4 temp=9+4 num=0 while loop breaks since num is not greater than 0 and 13 gets printed in num%10 ,operator "%" returns remainder
9th Jul 2020, 10:33 PM
Abhay
Abhay - avatar