Python Program to Print Hollow Mirrored Rhombus Star Pattern

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 Rhombus, the task is to print the Hollow Mirrored Rhombus Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the rhombus =8

Output:

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

Example2:

Input:

Given number of rows of the rhombus =6
Given character to print ='~'

Output:

6~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

Program to Print the Hollow Mirrored Rhombus Star Pattern in C, C++, and Python

Below are the ways to print the Hollow mirrored Rhonbus star Patterns in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the Hollow rhombus as static input and store it in a variable.
  • Loop till the number of rows of the rhombus using For loop.
  • Iterate till the first iterator using another For loop(Nested For loop).
  • Print the space character in the inner for loop.
  • Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
  • If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
  • If the current iterator’s value is equal to 0 or the number of rows-1, the print star with a space character.
  • Check the above two statements true or false using the If condition statement.
  • If it is true then print star with a space character.
  • Else print space.
  • Print the newline character after the end of two nested for loops.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 8
# Loop till the number of rows of the rhombus using For loop.
for m in range(rhombusrows):
  # Iterate till the first iterator using another For loop(Nested For loop).
    for n in range(m):
      # Print the space character in the inner for loop.
        print(' ', end='')
    # Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
    for o in range(rhombusrows):
      # If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
      # If the current iterator's value is equal to 0 or the number of rows-1, the print star with a space character.
      # Check the above two statements true or false using the If condition statement.
      # If it is true then print star with a space character.
      # Else print space.
        if(m == 0 or m == rhombusrows - 1 or o == 0 or o == rhombusrows - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after the end of two nested for loops.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 8;
    // Loop till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            cout << "  ";
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print star with a space
              character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0 || r == rhombusrows - 1)
                cout << "* ";
            else
                cout << "  ";
        }

        // Print the newline character after the end of two
        // nested for loops.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 8;
    // Loop till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            printf("  ");
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print star with a space
              character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0
                || r == rhombusrows - 1)
                printf("* ");
            else
                printf("  ");
        }

        // Print the newline character after the end of two
        // nested for loops.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the rhombus as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop till the number of rows of the rhombus using For loop.
  • Iterate till the first iterator using another For loop(Nested For loop).
  • Print the space character in the inner for loop.
  • Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
  • If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
  • If the current iterator’s value is equal to 0 or the number of rows-1, the print star with a space character.
  • Check the above two statements true or false using the If condition statement.
  • If it is true then print the given character with a space character.
  • Else print space.
  • Print the newline character after the end of two nested for loops.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the rhombus 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 rhombus as user input using int(input()) and store it in a variable.
rhombusrows = int(input(
    'Enter some random number of rows of rhombus = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop till the number of rows of the rhombus using For loop.
for m in range(rhombusrows):
  # Iterate till the first iterator using another For loop(Nested For loop).
    for n in range(m):
      # Print the space character in the inner for loop.
        print(' ', end='')
    # Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
    for o in range(rhombusrows):
      # If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
      # If the current iterator's value is equal to 0 or the number of rows-1, the print star with a space character.
      # Check the above two statements true or false using the If condition statement.
      # If it is true then print given character with a space character.
      # Else print space.
        if(m == 0 or m == rhombusrows - 1 or o == 0 or o == rhombusrows - 1):
            print(givencharacter, end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after the end of two nested for loops.
    print()

Output:

Enter some random number of rows of rhombus = 6
Enter some random character = ~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

2) C++ Implementation

  • Give the number of rows of the rhombus 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
    // Rhombbus as user input using cin and store it in a
    // variable.
    int rhombusrows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Rhombus = "
         << endl;
    cin >> rhombusrows;
    // 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 till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            cout << "  ";
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print given character with a
              space character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0
                || r == rhombusrows - 1)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }

        // Print the newline character after the end of two
        // nested for loops.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of rhombus = 6
Enter some random character = ~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

3) C Implementation

  • Give the number of rows of the Rhombus 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  Rhombus
    //  as user input using scanf and store it in a
    // variable.
    int rhombusrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &rhombusrows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            printf("  ");
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print given character with a
              space character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0
                || r == rhombusrows - 1)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }

        // Print the newline character after the end of two
        // nested for loops.
        printf("\n");
    }
    return 0;
}

Output:

6~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

Related Programs: