Computer Applications

Give the output of the following program segment:

for(r=1;r<=3;r++) 
{ for(c=1;c<r;c++) 
   { System.out.println(r); 
    } 
}

Java Iterative Stmts

2 Likes

Answer

Output
2
3
3
Explanation
  • When r = 1:

    • Inner loop runs for c from 1 to less than 1 → No iterations (because c < r is false at c=1)
    • So no output for r=1.
  • When r = 2:

    • Inner loop runs for c = 1 (since 1 < 2)
    • Prints 2 once.
  • When r = 3:

    • Inner loop runs for c = 1 and c = 2 (since c < 3)
    • Prints 3 twice.

Answered By

3 Likes


Related Questions