Python Program to Print Inverted Right Triangle Star Pattern

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

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

Examples:

Example1:

Input:

Given number of rows of the Inverted Right Triangle Pattern =13

Output:

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

Example2:

Input:

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

Output:

< < < < < < < < < 
< < < < < < < < 
< < < < < < < 
< < < < < < 
< < < < < 
< < < < 
< < < 
< < 
<

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

Below are the ways to print an 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 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 till the iterator value of the first loop using another nested For loop.
  • Print the star and space character in the inner for loop.
  • 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 Inverted Right Triangle as static input and store it in a variable.
triNumRows = 13
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the star and space character in the inner for loop.
        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 = 5;
    // 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 till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the star and space character in the
            // inner for loop.
            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 = 13;
    // 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 till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the star and space character in the
            // inner for loop.
            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 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 till the iterator value of the first loop using another nested For loop.
  • Print the given character and space character in the inner for loop.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the 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, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the given character with the space
        print(givencharacter, end=' ')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 7
Enter some random character = @
@ @ @ @ @ @ @ 
@ @ @ @ @ @ 
@ @ @ @ @ 
@ @ @ @ 
@ @ @ 
@ @ 
@

2) C++ Implementation

  • Give the number of rows of the 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 till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the given character with the space
            cout << givencharacter << " ";
        }
        // 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 = 
15
Enter some random character = 
^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ 
^ ^ ^ ^ 
^ ^ ^ 
^ ^ 
^

3) C Implementation

  • Give the number of rows of the 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 till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the given character and space character
            // in the inner for loop.
            printf("%c ", givencharacter);
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9<
< < < < < < < < < 
< < < < < < < < 
< < < < < < < 
< < < < < < 
< < < < < 
< < < < 
< < < 
< < 
<

Related Programs: