25.4k views
5 votes
Consider the following code segment. for (int a = 0; a < 10; a++) { for (int b = 10; b > a; b--) { System.out.print("#"); } } How many "#" symbols are printed as a result of executing the code segment?

User Shmygol
by
8.0k points

1 Answer

3 votes

Answer:

55

Step-by-step explanation:

Given the codes as below:

  1. for (int a = 0; a < 10; a++)
  2. {
  3. for (int b = 10; b > a; b--)
  4. {
  5. System.out.print("#");
  6. }
  7. }

There are two layer loops in the code. The outer loop (Line 1) will run for 10 iterations by traversing through a = 0 to a=9. However, the inner loop (Line 3) will run for 10 + 9 + 8 + 7 +...+ 1 = 55 iterations.

Since the print statement is within the inner loop (Line 5) and therefore the number of printed "#" symbols is dependent on the number of iterations of the inner loop. There will be 55 "#" symbols printed.

User Azerole
by
7.5k points