Python Program to Print Number Reduction Pattern

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Given the number of rows, the task is to print Number Reduction Pattern in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 4

Output:

1 2 3 4 
2 3 4 
3 4 
4

Example2:

Input:

Given number of rows = 14

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
2 3 4 5 6 7 8 9 10 11 12 13 14 
3 4 5 6 7 8 9 10 11 12 13 14 
4 5 6 7 8 9 10 11 12 13 14 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 
7 8 9 10 11 12 13 14 
8 9 10 11 12 13 14 
9 10 11 12 13 14 
10 11 12 13 14 
11 12 13 14 
12 13 14 
13 14 
14 

Program to Print Number Reduction Pattern in C, C++, and Python

Below are the ways to print Number Reduction Pattern in C, C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from iterator value of the parent For loop +1 to the number of rows using another for loop(Nested For loop).
  • Print the iterator value of the inner for loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbrrows = 9
# Loop from 0 to the number of rows using For loop.
for m in range(0, numbrrows+1):
    # Loop from iterator value of the parent For loop +1 to
    # the number of rows using another for loop(Nested For loop).
    for n in range(m + 1, numbrrows+1):
        # Print the iterator value of the inner for loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 2 3 4 5 6 7 8 9 
2 3 4 5 6 7 8 9 
3 4 5 6 7 8 9 
4 5 6 7 8 9 
5 6 7 8 9 
6 7 8 9 
7 8 9 
8 9 
9

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numbrrows = 10;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10 
2 3 4 5 6 7 8 9 10 
3 4 5 6 7 8 9 10 
4 5 6 7 8 9 10 
5 6 7 8 9 10 
6 7 8 9 10 
7 8 9 10 
8 9 10 
9 10 
10

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numbrrows = 14;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
2 3 4 5 6 7 8 9 10 11 12 13 14 
3 4 5 6 7 8 9 10 11 12 13 14 
4 5 6 7 8 9 10 11 12 13 14 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 
7 8 9 10 11 12 13 14 
8 9 10 11 12 13 14 
9 10 11 12 13 14 
10 11 12 13 14 
11 12 13 14 
12 13 14 
13 14 
14 

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from iterator value of the parent For loop +1 to the number of rows using another for loop(Nested For loop).
  • Print the iterator value of the inner for loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Give the number of rows as user input using int(input()) and store it in a variable.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbrrows = int(input('Enter some random number of rows = '))
# Loop from 0 to the number of rows using For loop.
for m in range(0, numbrrows+1):
    # Loop from iterator value of the parent For loop +1 to
    # the number of rows using another for loop(Nested For loop).
    for n in range(m + 1, numbrrows+1):
        # Print the iterator value of the inner for loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 6
1 2 3 4 5 6 
2 3 4 5 6 
3 4 5 6 
4 5 6 
5 6 
6

2) C++ Implementation

Give the number of rows as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;
int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numbrrows;
    cin >> numbrrows;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

4
1 2 3 4 
2 3 4 
3 4 
4

3) C Implementation

Give the number of rows as user input using scanf and store it in a variable.

Below is the implementation:

#include <math.h>
#include <stdio.h>
int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numbrrows;
    scanf("%d", &numbrrows);
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

4
1 2 3 4 
2 3 4 
3 4 
4

Related Programs: