Computer Applications
Give the output of the following program segment and also mention the number of times the loop is executed:
int x, y;
for (x = 9, y = 4; x <= 45; x+=9) {
if (x % y == 0)
break;
}
System.out.println(x);
Java
Java Iterative Stmts
22 Likes
Answer
36
Loop executes 4 times
Working
Below table shows the dry run of the loop:
| x | y | Remarks |
|---|---|---|
| 9 | 4 | 1st Iteration |
| 18 | 4 | 2nd Iteration |
| 27 | 4 | 3rd Iteration |
| 36 | 4 | 4th Iteration. As 36 % 4 is 0 so break statement terminates the loop. |
Answered By
15 Likes
Related Questions
Rewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );Define a class to accept a number and check whether it is a SUPERSPY number or not. A number is called SUPERSPY if the sum of the digits equals the number of the digits.
Example1:
Input: 1021 output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4, NUMBER OF DIGITS = 4 ]Example2:
Input: 125 output: Not an SUPERSPY number [1+2+5 is not equal to 3]