Computer Science, asked by siddharthsonisoni200, 10 months ago

1
33
555
7777
99999
python program by for loop?????

Answers

Answered by qwmillwall
2

Pattern

  • A for loop is used for iterating over a sequence.
  • For printing the given pattern we will use the nested for loop.
  • The outer loop iterates for the number of rows in the pattern.
  • The inner for loop keeps track of the number of characters to be printed in each row.

Python Code:

n = 5

x = 1

for i in range(5):

   for j in range(i):

       print(x, end='')

   x += 2

   print()

Output:

1

33

555

7777

99999

#SPJ2

Answered by vishakasaxenasl
4

Answer:

The following Python program performs the desired function:

rows = 5

start= 1

for i in range(rows):

  for j in range(i):

      print(start, end='' ")

  start= start+ 2

  print("\n")

Explanation:

Let's understand the above code:

  • Our task is to print a right triangle that contains odd number repetitions as per the row number.
  • First, we create a variable named row and initialize it to 5 since we need to point only 5 rows.
  • Then we start printing the triangle with the number 1.
  • After that, we increment the start variable with 2 so that we have the next odd number ie. 1+2 = 3
  • In row 2 we print two times the odd number 3.
  • Similarly, in row 3, we print three times the odd number 5.
  • In the last row, we print five times the odd number 9.

#SPJ2

Similar questions