Computer Science, asked by vanishreeharshaas, 11 months ago

Write a program to print pattern like :
2 1
3 21
4 3 2 1
5 4 3 2 1​

Answers

Answered by AbhijithPrakash
7

This pattern is exactly same as the pattern where 1 is also present.

For the pattern;

1\\2\: 1\\3\: 2\: 1\\4 \:3 \:2\: 1\\5\: 4\: 3\: 2\: 1

We use;

  • for i in range(1, 6): ## The outer loop starting from 1 to 6. For deciding the number of rows.
  •    for j in range(i, 0, -1):  # The inner loop for deciding the order of the column, i.e., when the value of "i" is 3, the value of "j" can get the values 3, 2, 1.
  •        print(j, end=' ')  # We'll print "j".
  •    print()

Now for your pattern;

2\:1\\3\:2\:1\\4\:3\:2\:1\\5\:4\:3\:2\:1

Here the value of "i" can be altered in the loop;

  • for i in range(2, 6): ## The outer loop starting from 2 to 6. For deciding the number of rows.
  •    for j in range(i, 0, -1):  # The inner loop for deciding the order of the column, i.e., when the value of "i" is 3, the value of "j" can get the values 3, 2, 1.
  •        print(j, end=' ')  # Rest all will be same.
  •    print()

Attachments:
Answered by Oreki
49

\text{\large\bf Java}

    \texttt{for (int i = 1; i < 5; i++) \{}\\\texttt{\hspace{1.5em} for (int j = i + 1; j > 0; j--)}\\\texttt{\hspace{3em} System.out.print(j + " ");}\\\texttt{\hspace{1.5em} System.out.println();}\\\texttt{\}}

\text{\large\bf Python}

    \texttt{for i in range(2, 6):}\\\texttt{\hspace{1.5em} print(*list(range(i, 0, -1)), sep=" ")}    

   

Similar questions