Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Given the length, breadth of the rectangle the task is to print the hollow rectangle star pattern in C, C++, and Python.
Examples:
Example1:
Input:
given length of rectangle =11 given breadth of rectangle =19
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example2:
Input:
given length of rectangle =20 given breadth of rectangle =5
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Â
Program to Print Hollow Rectangle Star Pattern in C, C++, and Python
Below are the ways to print the Hollow 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.
- We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
- If it is true then print * else print space.
- 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 = 11 breadthnum = 19 # 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): # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1. # If it is true then print * else print space. if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1): print('*', end=' ') else: 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++) { // We use the If Else statement to check If the // length or breadth number is 0 or maximum – 1. // If it is true then print * else print space. if (m == 0 || m == lengthnum - 1 || n == 0 || n == breadthnum - 1) cout << "* "; else 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 = 20; int breadthnum = 5; // 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++) { // We use the If Else statement to check If the // length or breadth number is 0 or maximum – 1. // If it is true then print * else print space. if (m == 0 || m == lengthnum - 1 || n == 0 || n == breadthnum - 1) printf("* "); else 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.
- We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
- If it is true then print * else print space.
- 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): # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1. # If it is true then print * else print space. if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1): print('*', end=' ') else: print(' ', end=' ') print()
Output:
Enter some random length value of the rectangle = 20 Enter some random breadth value of the rectangle = 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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++) { // We use the If Else statement to check If the // length or breadth number is 0 or maximum – 1. // If it is true then print * else print space. if (m == 0 || m == lengthnum - 1 || n == 0 || n == breadthnum - 1) cout << "* "; else cout << " "; } cout << endl; } return 0; }
Output:
Enter some random length and breadth of rectangle separated by spaces 11 19 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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++) { // We use the If Else statement to check If the // length or breadth number is 0 or maximum – 1. // If it is true then print * else print space. if (m == 0 || m == lengthnum - 1 || n == 0 || n == breadthnum - 1) printf("* "); else printf(" "); } printf("\n"); } }
Output:
Enter some random length and breadth of rectangle separated by spaces 11 23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Here we printed two blank spaces if the else condition is executed.