Python Program to Print Plus Star Pattern

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows, the task is to print plus star Patterns in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows = 9

Output:

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

Example2:

Input:

given number of rows =5
given character to print ='<'

Output:

5<
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

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

Below are the ways to Print Plus Star Pattern in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to 2*number of rows using For loop.
  • Loop from 1 to 2*number of rows using another For loop(Nested For loop).
  • Check if the parent loop iterator value is equal to the number of rows using the If statement.
  • Check if the inner loop iterator value is equal to the number of rows using the If statement.
  • Merge these both statements using or operator.
  • If this statement is true then print star character.
  • Else print space character.
  • Print the newline Character after the end of the inner for loop.
  • 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.
numberows = 9
# Loop from 1 to 2*number of rows using For loop.
for m in range(1, 2*numberows):
    # Loop from 1 to 2*number of rows using another For loop(Nested For loop).
    for n in range(1, 2*numberows):
        '''Check if the parent loop iterator value is equal to the number of rows using the If statement.
        Check if the inner loop iterator value is equal to the number of rows using the If statement.
        Merge these both statements using or operator.'''
        if(m == numberows or n == numberows):
            # If this statement is true then print star character.
            print('*', end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')

    # Print the newline character after the end of the inner For loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows as static input and store it
    // in a variable.
    int numberows = 9;

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print star
                // character.
                cout << "* ";
            else
                // Else print space character.
                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 as static input and store it
    // in a variable.
    int numberows = 9;

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print star
                // character.
                printf("* ");
            else
                // Else print space character.
                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 as user input and store it in a variable.
  • Give the character as user input and store it in a variable.
  • Loop from 1 to 2*number of rows using For loop.
  • Loop from 1 to 2*number of rows using another For loop(Nested For loop).
  • Check if the parent loop iterator value is equal to the number of rows using the If statement.
  • Check if the inner loop iterator value is equal to the number of rows using the If statement.
  • Merge these both statements using or operator.
  • If this statement is true then print the given character.
  • Else print space character.
  • Print the newline Character after the end of the inner for loop.
  • 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.

Below is the implementation:

# Give the word as user input using int(input()) and store it in a variable.
numberows = 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 2*number of rows using For loop.
for m in range(1, 2*numberows):
    # Loop from 1 to 2*number of rows using another For loop(Nested For loop).
    for n in range(1, 2*numberows):
        '''Check if the parent loop iterator value is equal to the number of rows using the If statement.
        Check if the inner loop iterator value is equal to the number of rows using the If statement.
        Merge these both statements using or operator.'''
        if(m == numberows or n == numberows):
            # If this statement is true then print givencharacter
            print(givencharacter, end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')

    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows = 5
Enter some random character = <
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

2) C++ Implementation

Give the number of rows as user input using cin and store it in a 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 numberows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> numberows;
    // 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 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print
                // givencharacter

                cout << givencharacter << " ";
            else
                // Else print space character.
                cout << "  ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 5
Enter some random character = <
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

3) C Implementation

Give the number of rows as user input using scanf and store it in a 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 numberows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &numberows);
    scanf("%c", &givencharacter);
    printf("\n");

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print
                // givencharacter
                printf("%c ", givencharacter);
            else
                // Else print space character.
                printf("  ");
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}

Output:

5<
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

Related Programs: