Python Program to Print Mirrored Right Triangle Star Pattern

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Given the number of rows of the right triangle, the task is to print Mirrored Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the right triangle =6

Output:

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

Example2:

Input:

given number of rows of the right triangle =
Given character to print ='$'

Output:

               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

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

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

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the right triangle pattern as static input and store it in a variable.
  • Iterate from 1 to given rows using the First For loop.
  • Iterate from 1 to given rows using another for loop(Nested For loop)
  • Check if the iterator value of the inner for loop is less than or equal to given rows – first iterator value using If statement.
  • If the statement is true then print space.
  • Else print the star character with space.
  • Print the newline character after the exit of the inner for loop.
  • The Exit of the Program.

1) Python Implementation:

Below is the implementation:

# Give the number of rows of the right triangle pattern as static input and store it in a variable.
triNumRows = 6
# Iterate from 1 to given rows using the First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
        # Check if the iterator value of the inner for loop is less than or equal to given rows - first iterator value using If statement.
        if(n <= triNumRows - m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print('*', end=' ')
    #Print the newline character after the exit of the 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 triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 6;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print star character with space.
                cout << "* ";
            }
        }
        // Print the newline character after the exit 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 right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 6;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("* ");
            }
        }
        // Print the newline character after the exit 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 right triangle pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Iterate from 1 to given rows using the First For loop.
  • Iterate from 1 to given rows using another for loop(Nested For loop)
  • Check if the iterator value of the inner for loop is less than or equal to given rows – first iterator value using If statement.
  • If the statement is true then print space.
  • Else print the star character with space.
  • Print the newline character after the exit of the inner for loop.
  • The Exit of the Program.

1) Python Implementation:

  • Give the number of rows of the 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 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 Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Iterate from 1 to given rows using First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
        # Check if the iterator value of the inner for loop is less than or equal to given rows - first iterator value using If statement.
        if(n <= triNumRows - m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of the Right Triangle Pattern = 6
Enter some random character = $
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

2) C++ Implementation

  • Give the number of rows of the 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 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;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print star character with space.
                cout <<givencharacter<<" ";
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Right Triangle Pattern = 
6
Enter some random character = 
$
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

3) C Implementation

  • Give the number of rows of the 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  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");
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("%c ", givencharacter);
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

6$
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

Related Programs: