Python Program to Print Pyramid 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 pyramid, the task is to print the Pyramid Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the pyramid  =10

Output:

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

Example2:

Input:

Given number of rows of the pyramid  =10
Given character to print='^'

Output:

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

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

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

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the pyramid 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-1 of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value+1 of the parent For loop using another Nested For loop(Inner For loop).
  • Print the star character with a space character in the inner For loop.
  • After the end of the two 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 pyramid as static input and store it in a variable.
pyRows = 10
# Loop from the number of rows to 0 in decreasing order using For Loop.
# Loop from 0 to the number of rows using For Loop.
for m in range(0, pyRows):
    # Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    # using another Nested For loop(Inner For loop).
    for n in range(0, pyRows-m-1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the iterator value+1 of the parent For loop
    # using another Nested For loop(Inner For loop).

    for l in range(0, m+1):
        # Print the star character with a star character in the inner For loop.
        print('*', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from 0 to the number of rows using For Loop.
    for (int m = 0; m < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << "* ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from 0 to the number of rows using For Loop.
    for (int m = 0; m < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            printf("* ");
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the pyramid as user input and store it in a variable.
  • Give the character as static input and store it in a variable.
  • 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 Nested For loop(Inner 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 Nested For loop(Inner For loop).
  • Print the given character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

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

    for l in range(0, m+1):
        # Print the given character with a space character in the inner For loop.
        print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of pyramid = 10 
Enter some random character = ^
                 ^ 
               ^ ^ 
             ^ ^ ^ 
           ^ ^ ^ ^ 
          ^ ^ ^ ^ ^ 
        ^ ^ ^ ^ ^ ^ 
      ^ ^ ^ ^ ^ ^ ^ 
    ^ ^ ^ ^ ^ ^ ^ ^ 
  ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^

2) C++ Implementation

  • Give the number of rows of the pyramid as user input using scanf 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 pyRows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> pyRows;
    // 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 < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the given character with a space
            // character in the inner For loop.
            cout <<givencharacter<<" ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of pyramid = 
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 pyRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &pyRows);
    scanf("%c", &givencharacter);
    // Loop from 0 to the number of rows using For Loop.
    for (int m = 0; m < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the given  character with a space
            // character in the inner For loop.
            printf("%c ", givencharacter);
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs: