Python Program to Print Hollow Right Triangle Star Pattern

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Given the number of rows of the right-angled Hollow triangle star pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the hollow right-angled triangle star pattern=11

Output:

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

Example2:

Input:

given number of rows of the hollow right-angled triangle star pattern=8
given character to print =^

Output

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

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

Below are the ways to print Hollow 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 right-angled triangle star pattern as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • If you closely examine the pattern, you will notice that the star is available on the first or last column or row. So, for the first or last column or row, print a star, otherwise, print space.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the hollow right-angled triangle star pattern
# as static input and store it in a variable.
trianglerows = 7
# Loop from 1 to the number of rows using For loop.
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # If you closely examine the pattern, you will notice that the
        # star is available on the first or last column or row.
        # So, for the first or last column or row, print a star, otherwise, print space.
        if(m == 1 or m == trianglerows or n == 1 or n == m):
           print('*', end=' ')
        else:
           print(' ', end=' ')
    # Print the newline character after ending of 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 right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 11;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after ending of 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 right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 7;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character )

Approach:

  • Give the number of rows of the right-angled triangle star pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • If you closely examine the pattern, you will notice that the star is available on the first or last column or row. So, for the first or last column or row, print a star, otherwise, print space.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

  • Give the number of rows of the hollow right-angled 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 right-angled triangle as user input using int(input()) and store it in a variable.
trianglerows = int(
    input('Enter some random number of rows of the right-angled triangle = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
         # If you closely examine the pattern, you will notice that the
        # star is available on the first or last column or row.
        # So, for the first or last column or row, print a star, otherwise, print space.
        if(m == 1 or m == trianglerows or n == 1 or n == m):
            print(givencharacter, end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output

Enter some random number of rows of the right-angled triangle = 8
Enter some random character = ^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

2) C++ Implementation

  • Give the number of rows of the right-angled 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()
{
    int trianglerows;
    char givencharacter;

    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    cout << "Enter some random number of rows of the "
            "right-angled triangle = "
         << endl;
    cin >> trianglerows;
    // 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 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }
    return 0;
}

Output

Enter some random number of rows of the right-angled triangle = 8
Enter some random character = ^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

3) C Implementation

  • Give the number of rows of the right-angled 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()
{

    int trianglerows;
    char givencharacter;
    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    scanf("%d", &trianglerows);

    // Give the Character as user input using cin and store
    // it in another variable.
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output

8^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

Related Programs: