KnowledgeBoat Logo
|

Computer Applications

Give the output of the following program segment:

for(k=a;k<=a*b;k+=a) 
{ if (k%b==0) 
   break; 
} 
System.out.println(k);

Give the output when a = 6, b = 4.

Java Iterative Stmts

1 Like

Answer

Output
12
Explanation

1. The loop starts with k = 6.

2. The loop runs while k <= 6 * 4, i.e., k <= 24.

3. In each iteration, k increases by 6.

4. The loop checks if k % 4 == 0. If true, it breaks the loop.

Iterations:

  • First: k = 6, 6 % 4 = 2 → Not 0 → Loop continues
  • Second: k = 12, 12 % 4 = 0 → Condition true → Loop breaks

5. The current value of k is 12. So, the output is: 12.

Answered By

1 Like


Related Questions