Python Program to Print Hollow Diamond Star Pattern

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Given the number of rows of the diamond pattern, the task is to print the Hollow diamond star pattern in C, C++, and Python

Examples:

Example1:

Input:

given number of rows of diamond =6

Output:

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

Example2:

Input:

given number of rows of diamond =10
given character to print ='^'

Output:

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

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

Below are the ways to print Diamond Star Pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the diamond 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 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 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print star character.
  • Else print space character.
  • After the end of the inner for Loops print the Newline Character.
  • loop from the number of rows -1 to 0 in decreasing order using For loop.
  • Loop from 1 to number of rows- iterator value of the parent loop using another For loop(Nested For loop)
  • Print the space character in the inner For loop.
  • Loop from 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print star character.
  • Else print space 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  as static input and store it in a variable.
diamondrows = 6

# Loop from 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print star character.
        if l == 1 or l == m * 2 - 1:
            print('*', end='')
        # Else print space character.
        else:
            print(' ', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# loop from number of rows -1 to 0 in decreasing order using For loop.
for m in range(diamondrows - 1, 0, -1):
    # Loop from 1 to number of rows- iterator value
    # of the parent loop using another For loop(Nested For loop)
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print star character.
        if l == 1 or l == m * 2 - 1:
            print('*', end='')
        # Else print space character.
        else:
            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 = 6;
    char givencharacter = '*';
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

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

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
  // loop from number of rows -1 to 0 in decreasing order using For loop.

    for (int m = diamondrows-1; m > 0; m--) {
    //Loop from 1 to number of rows- iterator value
    // of the parent loop using another For loop(Nested For loop)
        for (int n = 1; n <= diamondrows-m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                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()
{
    char givencharacter = '*';
    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.
    int diamondrows = 6;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

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

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // loop from number of rows -1 to 0 in decreasing order
    // using For loop.

    for (int m = diamondrows - 1; m > 0; m--) {
        // Loop from 1 to number of rows- iterator value
        // of the parent loop using another For loop(Nested
        // For loop)
        for (int n = 1; n <= diamondrows - m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                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 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 1 to the number of rows using For Loop.
  • Loop from 1 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 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print given character.
  • Else print space character.
  • After the end of the inner for Loops print the Newline Character.
  • loop from the number of rows -1 to 0 in decreasing order using For loop.
  • Loop from 1 to number of rows- iterator value of the parent loop using another For loop(Nested For loop)
  • Print the space character in the inner For loop.
  • Loop from 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print given character.
  • Else print space 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 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print givencharacter .
        if l == 1 or l == m * 2 - 1:
            print(givencharacter, end='')
        # Else print space character.
        else:
            print(' ', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# loop from number of rows -1 to 0 in decreasing order using For loop.
for m in range(diamondrows - 1, 0, -1):
    # Loop from 1 to number of rows- iterator value
    # of the parent loop using another For loop(Nested For loop)
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print givencharacter .
        if l == 1 or l == m * 2 - 1:
            print(givencharacter, end='')
        # Else print space character.
        else:
            print(' ', end='')

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

Output:

Enter some random number of rows = 10
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 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

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

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
  // loop from number of rows -1 to 0 in decreasing order using For loop.

    for (int m = diamondrows-1; m > 0; m--) {
    //Loop from 1 to number of rows- iterator value
    // of the parent loop using another For loop(Nested For loop)
        for (int n = 1; n <= diamondrows-m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }

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

Output:

Enter some random number of rows = 
10
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);
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

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

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // loop from number of rows -1 to 0 in decreasing order
    // using For loop.

    for (int m = diamondrows - 1; m > 0; m--) {
        // Loop from 1 to number of rows- iterator value
        // of the parent loop using another For loop(Nested
        // For loop)
        for (int n = 1; n <= diamondrows - m; n++) {
            // Print the space character in the inner For
            // loop.
           printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }

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

Output:

10^
          ^
        ^ ^
       ^   ^
      ^     ^
     ^       ^
    ^         ^
   ^           ^
  ^             ^
 ^               ^
^                 ^
 ^               ^
  ^             ^
   ^           ^
    ^         ^
     ^       ^
      ^     ^
       ^   ^
        ^ ^
         ^

Related Programs: