Python Program to Print Hollow Inverted Right Triangle

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows of the Triangle, the task is to Print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =7

Output:

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

 

Example2:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =9
Given character to print ='^'

Output:

^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

Program to Print Hollow Inverted Right Triangle Star Pattern in C, C++, and Python

Below are the ways to print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Method #1: Using For Loop(Star Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
triNumRows = 7
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print('*', end='')
        else:
            print(' ', end='')
    # Print the newline character after inner for loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle 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 Inverted Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Inverted Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print(givencharacter, end='')
        else:
            print(' ', end='')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

2) C++ Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle as user input using cin 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 of the Inverted Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // 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 given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

3) C Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.

    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

Related Programs: