Python Program to Print Inverted Pyramid Pattern with the Same Digit

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Given the number of rows, the task is to print Inverted Pyramid Pattern with the same digit in C,C++, and Python

Examples:

Example1:

Input:

Given number of rows = 10

Output:

10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 10 
10

Example2:

Input:

Given number of rows = 9

Output:

9 9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 
9 9 9 9 9 
9 9 9 9 
9 9 9 
9 9 
9

Program to Print Inverted Pyramid Pattern with the Same Digit in C, C++, and Python

Below are the ways to Program to Print Inverted Pyramid patterns with the Same Digit 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.
  • Take a variable and store the given number of rows in it say givennumbrows.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 to the parent loop iterator using another For loop(Nested For Loop).
  • Print the givennumbrows.
  • 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 = 7
# Take a variable and store the given number of rows in it say givennumbrows.
givennumbrows = numbrrows
# Loop from the number of rows to 0 in decreasing order using For loop.
for m in range(numbrrows, 0, -1):
    # Loop from 0 to the parent loop iterator using another For loop(Nested For Loop).
    for n in range(0, m):
        # Print the givennumbrows.
        print(givennumbrows, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

7 7 7 7 7 7 7 
7 7 7 7 7 7 
7 7 7 7 7 
7 7 7 7 
7 7 7 
7 7 
7

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;
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            cout << givennumbrows << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 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 = 4;
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            printf("%d ", givennumbrows);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

4 4 4 4 
4 4 4 
4 4 
4

Method #2: Using For Loop (User Input)

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 = '))
# Take a variable and store the given number of rows in it say givennumbrows.
givennumbrows = numbrrows
# Loop from the number of rows to 0 in decreasing order using For loop.
for m in range(numbrrows, 0, -1):
    # Loop from 0 to the parent loop iterator using another For loop(Nested For Loop).
    for n in range(0, m):
        # Print the givennumbrows.
        print(givennumbrows, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 10
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 10 
10

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;
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            cout << givennumbrows << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

10
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 10 
10

3) C Implementation

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

Below is the implementation:

#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);
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            printf("%d ", givennumbrows);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

9
9 9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 
9 9 9 9 9 
9 9 9 9 
9 9 9 
9 9 
9

Related Programs: