Computer Science, asked by loganihrishi, 6 months ago

write a python program to print the following pattern.
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1​

Answers

Answered by rajutusharengineerin
8

Answer:

Explanation:We have to make a python script for these patterns;

We'll have to make loops for making these patterns.

For pattern (a):

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Here we have 5 rows and columns, so we'll use 2 nested "for" loops;

To begin our code we'll start with the for loop with i as the variable;

So, the code will look like ;

for i in range(1,6):

This will be the outer loop.

So now let's try to create the inner loop;

We will be using the j as the variable.

The code will be;

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

The "i+1" present inside the parentheses is to change values according to the outer loop.

Let's combine the 2 codes.

We'll get

\textrm{for i in range(1,6):}\\\textrm{ for j in range(1, i+1):}

Now let's start printing the values;

We will use the j variable to print as it will increase numbers at each column.

The code will be,

print(j, end=" ")

We have to use the end parameter so that all the required values come in rows.

And this print function will be inside the inner loop.

Combine the code;

\textrm{for i in range(1,6):}\\\textrm{\quad for j in range(1, i+1):}\\\textrm{\quad \quad print(j, end=" ")}

But if we run the code all the number will be printed in one line, and that's not we want, so we will use "print("\r")" or "print()" bellow the outer loop.

So, the entire code will be;

\textrm{for i in range(1,6):}\\\textrm{\quad for j in range(1, i+1):}\\\textrm{\quad \quad print(j, end=" ")}\\\textrm{\quad print()}

for i in range(1,6):

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

  print(j, end=" ")

print()

Now let's start (b)

* * * * *

* * * *

* * *

* *

*

Well, the code will be same but the number's in the parentheses will be different and instead of j we will print *;

Here is the code;

\textrm{for i in range(5,0,-1):}\\\textrm{\quad for j in range(i,0,-1):}\\\textrm{\quad \quad print("*", end=" ")}\\\mathrm{\quad print()}

for i in range (5,0,-1):

  for j in range (i,0,-1):

      print("*", end="")

  print()

PLS MARK IT AS BRAINLIEST

Answered by hazrautu19
2

Explanation:

Simple pyramid pattern

# Python 3.x code to demonstrate star pattern

# Function to demonstrate printing pattern

def pypart(n):

# outer loop to handle number of rows

# n in this case

for i in range(0, n):

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# printing stars

print("* ",end="")

# ending line after each row

print("\r")

# Driver Code

n = 5

pypart(n)

Output:

*

* *

* * *

* * * *

* * * * *

think so,same type

Similar questions