Computer Science, asked by baivabiqwerty235, 7 months ago

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5​

Answers

Answered by anindyaadhikari13
5

Correct Question:-

Write a program for the following pattern.

5 4 3 2 1

5 4 3 2

5 4 3

5 4

5

Program:-

This is written in Java.

class Pattern

{

public static void main(String args[])

{

for(int i=1;i<=5;i++)

{

for(int j=5;j>=i;j--)

System.out.print(j+" ");

System.out.println();

}

}

}

In Python.

for i in range(1,6):

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

print(j, end=" ")

print()

Answered by mahinderjeetkaur878
0

Here is a simple Python program that will print the desired pattern:

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

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

print(j, end=' ')

print()

The code above uses two nested for loops to print the desired pattern:

  • The outer loop iterates over the numbers 5 to 1, using the range function with a start value of 5, an end value of 0 (not inclusive), and a step of -1 (to decrement the loop variable on each iteration). This means that the loop will run 5 times, with i taking on the values 5, 4, 3, 2, and 1 in each iteration.

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

  • The inner loop is nested inside the outer loop, and iterates over the numbers 5 to i, again using the range function. The start value is 5, the end value is i-1 (not inclusive), and the step is -1. This means that the loop will run for a decreasing number of times on each iteration of the outer loop, printing out the numbers in decreasing order each time.

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

  • Inside the inner loop, the print() function is called with the current value of j, followed by the end=' ' argument. This causes the numbers to be printed on the same line, with a space between each number.

print(j, end=' ')

  • After the inner loop completes, a new line is printed using the print() function with no arguments. This moves the output to the next line for the next iteration of the outer loop.

print()

  • The outer loop then continues to the next iteration, and the process repeats until the loop is complete and the entire pattern has been printed.

The output of this code will be:

5 4 3 2 1

5 4 3 2

5 4 3

5 4

5

The question you might be looking for :-

Write a program that will print the following pattern.

5 4 3 2 1

5 4 3 2

5 4 3

5 4

5

To know more: -

https://brainly.in/question/6873338?referrer=searchResults

https://brainly.in/question/7109244?referrer=searchResults

#SPJ3

Similar questions