What will be the output of the following code:

n = 5

for i in range(1, n+1):

  for j in range(i):

    print("*", end="")

  print()

Options:

a.*

   **

   ***

   ****

   *****

b.*****

   ****

   ***

   **

   *

c.* * * * *

   * * * *

   * * *

   * *

   *

d.None of the above

Answer:

Answer by student

a.*

**

***

****

*****

Detailed answer by teachoo

-lock-

Let’s go through each of the options and see why they are correct or incorrect.

  • Option a.

*

**

***

****

*****

This is the correct answer. We will explain how to get this output in the next steps.

  • Option b.

*****

****

***

**

*

This is incorrect . This would be the output if we reversed the order of the range function in the inner loop, i.e. for j in range(i, 0, -1).

  • Option c.

* * * * *

* * * *

* * *

* *

*

This is incorrect . This would be the output if we added a space after each asterisk in the print function, i.e. print(“* “, end=””).

  • Option d. None of the above: This is incorrect . One of the above options is correct, as we have seen.

Now, let’s see how to get the correct output by following the code step by step.

  • The first line of code assigns the value 5 to n: 

n = 5

  • The second line of code starts a for loop that iterates over the range(1, n+1), which is range(1, 6) or [1, 2, 3, 4, 5]. 

This means that i will take each of these values one by one: for i in range(1, n+1)

  • The third line of code starts another for loop inside the first loop that iterates over the range(i), which is range(0, i) or [0, 1, …, i-1]. 

This means that j will take each of these values one by one for each value of i: for j in range(i)

  • The fourth line of code prints an asterisk without moving to a new line, using the end=“” parameter.

This means that for each value of j, an asterisk will be printed on the same line: print(“*”, end=“”)

  • The fifth line of code prints a new line after each iteration of the inner loop. 

This means that after printing i asterisks on one line, the cursor will move to the next line: print()

So, what happens when we run this code?

  • In the first iteration of the outer loop, i = 1. Then, in the inner loop, j = 0. 

So, we print one asterisk on the first line and then move to the next line.

  • In the second iteration of the outer loop, i = 2. Then, in the inner loop, j = 0 and j = 1.

So, we print two asterisks on the second line and then move to the next line.

  • In the third iteration of the outer loop, i = 3. Then, in the inner loop, j = 0, j = 1 and j = 2. 

So, we print three asterisks on the third line and then move to the next line.

  • In the fourth iteration of the outer loop, i = 4. Then, in the inner loop, j = 0, j = 1, j = 2 and j = 3. 

So, we print four asterisks on the fourth line and then move to the next line.

  • In the fifth and final iteration of the outer loop, i = 5. Then, in the inner loop, j = 0, j = 1, j = 2, j = 3 and j = 4. 

So, we print five asterisks on the fifth line and then move to the next line.

The output looks like this:

*

**

***

****

*****

So, this matches option a.

So, the correct answer is option a.

*

**

***

****

*****

-endlock-

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.