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

Examples:

Example1:

Input:

given number of rows of diamond =5

Output:

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

Example2:

Input:

given number of rows of diamond =7
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 0 to the 2* iterator value – 1 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 1 to the number of rows using For loop.
  • Loop from 1 to 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 2*(number of rows – 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 = 5
# 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 0 to the 2* iterator value - 1 of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, (2 * m) - 1):
        # Print the star character
        print('*', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# Loop from 1 to the number of rows using For loop.
for m in range(1, diamondrows):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for n in range(1, m+1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to 2*(number of rows - iterator value of the parent for loop)
    # using another For loop(Nested For loop).
    for l in range(1, (2 * (diamondrows - 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 = 5;
    // 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 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the star character
            cout << "*";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - 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 = 5;
    // 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 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the star character
            printf("*");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - 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 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 0 to the 2* iterator value – 1 of the parent For loop using another For loop(Nested For loop).
  • Print the given character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to 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 2*(number of rows – iterator value of the parent for loop) using another For loop(Nested For loop).
  • Print the given 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 0 to the 2* iterator value - 1 of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, (2 * m) - 1):
        # Print the given character
        print(givencharacter, end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

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