Python Program to Print Mirrored Half Diamond 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 diamond pattern, the task is to print the Mirrored Half diamond Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =9

Output:

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

Example2:

Input:

Given number of rows =7
Given Character to print ='+'

Output:

               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

Program to Print Mirrored Half Diamond Star Pattern in C, C++, and Python

Below are the ways to print Mirrored Half Diamond Star Pattern in C, C++, and python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the number of diamond patterns as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the diamond pattern as static input and store it in a variable.
diamondrows = 9
# Loop from 0 to the number of rows using For Loop.
for m in range(0, diamondrows):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the star character
        print('*', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()
# Loop from number of rows to 0 in decreasing order using For loop.
for m in range(diamondrows, 0, -1):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the star character
        print('*', end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 9;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            cout << "*";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            cout << "*";
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 9;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            printf("*");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            printf("*");
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the number of diamond patterns as static input and store it in a variable.
  • Give the character as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows 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  as user input using int(input()) and store it in a variable.
diamondrows = int(input(
    'Enter some random number of rows  = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 0 to the number of rows using For Loop.
for m in range(0, diamondrows):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the givencharacter
        print(givencharacter, end='')
    # After the end of the inner for Loops print the Newline Character.
    print()
# Loop from number of rows to 0 in decreasing order using For loop.
for m in range(diamondrows, 0, -1):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
         # Print the givencharacter
        print(givencharacter, end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

Enter some random number of rows = 7
Enter some random character = +
               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

2) C++ Implementation

  • Give the number of rows 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
    // as user input using cin and store it in a
    // variable.
    int diamondrows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> diamondrows;
    // 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 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            cout << givencharacter;
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            cout << givencharacter;
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 7
Enter some random character = +
               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

3) C Implementation

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

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            printf("%c", givencharacter);
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            printf("%c", givencharacter);
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

7+
               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

Related Programs: