Python Program to Print Rectangle 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 length, breadth of the rectangle the task is to print the rectangle star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given length of rectangle =8
given breadth of rectangle =29

Output:

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

Example2:

Input:

given length of rectangle =19
given breadth of rectangle =5

Output:

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

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

Below are the ways to print the rectangle star pattern in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the length and breadth as static input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the length and breadth as static input and store them in two variables.
lengthnum = 8
breadthnum = 11
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 11;
    int breadthnum = 23;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;n++) { // Print the * character inside the
                    // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 9;
    int breadthnum = 29;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the length and breadth as user input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Give the length and breadth of the rectangle as user input using the int(input()) function and store them in two separate variables.

Below is the implementation:

# Give the length and breadth of the rectangle as user input using the int(input()) function
# and store them in two separate variables.
lengthnum = int(input('Enter some random length value of the rectangle = '))

breadthnum=int(input('Enter some random breadth value of the rectangle = '))
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

Enter some random length value of the rectangle = 9
Enter some random breadth value of the rectangle = 4
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 

2) C++ Implementation

Give the length and breadth of the rectangle as user input using the cin and store them in two separate variables.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth of the rectangle as user
    // input using the cin
    // and store them in two separate variables.
    int lengthnum, breadthnum;
    cout << "Enter some random length and breadth of "
            "rectangle separated by spaces"
         << endl;
    cin >> lengthnum >> breadthnum;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;
             n++) { // Print the * character inside the
            // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random length and breadth of rectangle separated by spaces
4 16
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 

3) C Implementation

Give the length and breadth of the rectangle as user input using the scanf and store them in two separate variables.

Below is the implementation:

#include <stdio.h>

int main()
{
    //    Give the length and breadth of the rectangle as
    // user input using the scanf
    // and store them in two separate variables.
    int lengthnum,breadthnum ;
    printf("Enter some random length and breadth of rectangle separated by spaces \n");
    scanf("%d%d", &lengthnum, &breadthnum);
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

Enter some random length and breadth of rectangle separated by spaces
8 15
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * *

Related Programs: