What is the output of this program?
public class HW{
public static void main(String[] args){
int n = 365;
int sum = 0;
while(n > 0){
int digit = n % 10;
sum = sum + digit;
}
System.out.println(sum);
}
}
Answers
Answered by
2
Answer:
Output : Infinity ♾
Dry Run :
n = 365
sum = 0
digit = 0
1st Case :
digit = 365 % 10 = 36
sum = 0 + 36 = 36
2nd Case :
digit = 36 % 10 = 3
sum = 36 + 3 = 39
3rd Case :
digit = 3 % 10 = 3
sum = 39 + 3 = 41
4th Case :
digit = 3 % 10 = 3
sum = 41 + 3 = 44
5th, 6th, 7th . . . Case :
digit = 3 and so on . . . . .. Infinity times
sum = 44 + 3 + 3 + 3 + . . . . . . Infinity ♾
Mark me as the Brainliest Answer!!
Similar questions