Login Register Appointment

Left-Aligned Decreasing (n to 1)

Lesson 12/21 | Study Time: 15 Min
Course: Pattern Program

Pattern:


10 9 8 7

6 5 4

3 2

1


Solution

Note: Don't jump directly to the solution, try it out yourself first.


Pattern Breakdown

The total number of rows is 4.
The pattern has 4 rows, and each row contains decreasing numbers starting from 10.


To print this pattern, we need two loops:

-->Outer loop for the rows.

-->Inner loop for the columns (printing numbers in each row).

1) In each iteration of the outer loop:

2) The inner loop prints decreasing numbers starting from the current start_value.

3) The number of elements printed in each row is determined by total_rows - row.

4) After printing a number, the start_value is decremented.



Video Solution


Java


public class Main {

    public static void main(String[] args) {

        int total_rows = 4;  // Number of rows

        int start_value = 10;  // Starting value for the first row


        // Outer loop for rows

        for (int row = 0; row < total_rows; row++) {

            // Inner loop for columns (printing decreasing numbers)

            for (int col = 0; col < total_rows - row; col++) {

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

                start_value--;  // Decrease the number for the next column

            }

            System.out.println();  // Move to the next line

        }

    }

}




C++


#include <iostream>

using namespace std;

int main() {

    int total_rows = 4;  // Number of rows

    int start_value = 10;  // Starting value for the first row


    // Outer loop for rows

    for (int row = 0; row < total_rows; row++) {


        // Inner loop for columns (printing decreasing numbers)

        for (int col = 0; col < total_rows - row; col++) {

            cout << start_value << " ";

            start_value--;  // Decrease the number for the next column

        }

        cout << endl;  // Move to the next line

    }


    return 0;

}


Python


total_rows = 4  # Number of rows

start_value = 10  # Starting value for the first row


# Outer loop for rows

for row in range(total_rows):

    # Inner loop for columns (printing decreasing numbers)

    for col in range(total_rows - row):

        print(start_value, end=" ")

        start_value -= 1  # Decrease the number for the next column

    print()  # Move to the next line