Python Program to Print Inverted Pyramid Star Pattern

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Given the number of rows of the pyramid, the task is to print the Inverted Pyramid Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the pyramid  =10

Output:

* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         *

Example2:

Input:

Given number of rows of the pyramid  =10
Given character to print='&'

Output:

& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

Program to Print Inverted Pyramid Star Pattern in C, C++, and Python

Below are the ways to print Inverted Pyramid Star Pattern in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the pyramid as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the star character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the pyramid as static input and store it in a variable.
pyRows = 10
# Loop from the number of rows to 0 in decreasing order using For Loop.
for m in range(pyRows, 0, -1):
    # Loop from 0 to the number of rows - iterator value of the parent
    # For loop using another Nested For loop(Inner For loop)..(0, rows - i):
    for n in range(0, pyRows-m):
        # Print the space character in the inner For loop.
        print(end=' ')
     # Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).

    for l in range(0, m):
        # Print the star character with a space character in the inner For loop.
        print('*', end=' ')
    print()

Output:

* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         *

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows=10;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << "* ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         *

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            printf("* ");
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         *

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the pyramid as user input and store it in a variable.
  • Give the character as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the given character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

# Give the number of rows of the pyramid as user input using int(input()) and store it in a variable.
pyRows = int(input(
    'Enter some random number of rows of pyramid = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the number of rows to 0 in decreasing order using For Loop.
for m in range(pyRows, 0, -1):
    # Loop from 0 to the number of rows - iterator value of the parent
    # For loop using another Nested For loop(Inner For loop).
    for n in range(0, pyRows-m):
        # Print the space character in the inner For loop.
        print(end=' ')
     # Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).

    for l in range(0, m):
        # Print the given character with a space character in the inner For loop.
        print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of pyramid = 10
Enter some random character = &
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

2) C++ Implementation

  • Give the number of rows of the pyramid as user input using scanf and store it in a variable.
  • Give the Character as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows
    // as user input using cin and store it in a
    // variable.
    int pyRows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> pyRows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << givencharacter << " ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of pyramid = 
10
Enter some random character = 
&
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

3) C Implementation

  • Give the number of rows as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another 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 pyRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &pyRows);
    scanf("%c", &givencharacter);
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the given character with a space
            // character in the inner For loop.
            printf("%c ", givencharacter);
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

10&
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

Related Programs: