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

Examples:

Example1:

Input:

Given number of rows =7

Output:

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

Example2:

Input:

Given number of rows =9
Given Character to print ='-'

Output:

- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

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

Below are the ways to print Hollow 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 pattern as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop till the first iterator value using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to 0 using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print star character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • After the end of two For loops Loop from 1 to the number of rows using For loop.
  • Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to the number of rows using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print star character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the number of diamond pattern as static input and store it in a variable.
rowsnumber = 7
# Loop from 0 to the number of rows using For loop.
for m in range(0, rowsnumber):
        # Loop till the first iterator value using another For loop(Nested For loop).
    for n in range(0, m+1):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
        # Check if the second loop iterator value is equal to 0 using the If Statement.
        # Combine these two conditions using or operator.
        if(m == n or n == 0):
            # If the statement is true then print star character with space.
            print("*", end=" ")
          # Else print space character.
        else:
            print(' ', end=' ')
    # After the end of the inner for loop print the Newline Character.
    print()
# After the end of two For loops Loop from 1 to the number of rows using For loop.
for m in range(1, rowsnumber):
    # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
    for n in range(m, rowsnumber):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
                # Check if the second loop iterator value is equal to the number of rows using the If Statement.
                # Combine these two conditions using or operator.
        if(m == n or n == rowsnumber-1):
         # If the statement is true then print star character with space.
            print('*', end=' ')
          # Else print space character.
        else:
            print(' ', end=' ')

    # After the end of the inner for loop 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 number of diamond pattern  \
  //  as static input and store it in a variable.
    int rowsnumber = 7;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {
            /*
            Check if the first loop iterator value is equal
            to the second loop iterator value using the If
            Statement. Check if the second loop iterator
            value is equal to 0 using the If Statement.
              Combine these two conditions using or
            operator. If the statement is true then print
            star character with space. Else print space
            character.*/
            if (m == n || n == 0)
                cout << "* ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print star character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop 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 number of diamond pattern  \
  //  as static input and store it in a variable.
    int rowsnumber = 7;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {

            /*
           Check if the first loop iterator value is equal
           to the second loop iterator value using the If
           Statement. Check if the second loop iterator
           value is equal to 0 using the If Statement.
             Combine these two conditions using or
           operator. If the statement is true then print
           star character with space. Else print space
           character.*/
            if (m == n || n == 0)
                printf("* ");
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print star character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                printf("* ");
            else
                printf("  ");
        }
        // After the end of the inner for loop 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 pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop till the first iterator value using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to 0 using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print the given character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • After the end of two For loops Loop from 1 to the number of rows using For loop.
  • Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to the number of rows using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print the given character with space.
  • Else print space character.
  • After the end of the inner for loop 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.
rowsnumber = 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, rowsnumber):
        # Loop till the first iterator value using another For loop(Nested For loop).
    for n in range(0, m+1):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
        # Check if the second loop iterator value is equal to 0 using the If Statement.
        # Combine these two conditions using or operator.
        if(m == n or n == 0):
            # If the statement is true then print the given character with space.
            print(givencharacter, end=" ")
          # Else print space character.
        else:
            print(' ', end=' ')
    # After the end of the inner for loop print the Newline Character.
    print()
# After the end of two For loops Loop from 1 to the number of rows using For loop.
for m in range(1, rowsnumber):
    # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
    for n in range(m, rowsnumber):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
                # Check if the second loop iterator value is equal to the number of rows using the If Statement.
                # Combine these two conditions using or operator.
        if(m == n or n == rowsnumber-1):
         # If the statement is true then print the given character with space.
            print(givencharacter, end=' ')
          # Else print space character.
        else:
            print(' ', end=' ')

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

Output:

Enter some random number of rows = 9
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 rowsnumber;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> rowsnumber;
    // 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 < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {
            /*
            Check if the first loop iterator value is equal
            to the second loop iterator value using the If
            Statement. Check if the second loop iterator
            value is equal to 0 using the If Statement.
              Combine these two conditions using or
            operator. If the statement is true then print
            given character with space. Else print space
            character.*/
            if (m == n || n == 0)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print given character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows = 9
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 rowsnumber;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &rowsnumber);
    scanf("%c", &givencharacter);
 
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {

            /*
           Check if the first loop iterator value is equal
           to the second loop iterator value using the If
           Statement. Check if the second loop iterator
           value is equal to 0 using the If Statement.
             Combine these two conditions using or
           operator. If the statement is true then print
           given character with space. Else print space
           character.*/
            if (m == n || n == 0)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print given character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    return 0;
}

Output:

9-
- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

Related Programs: