Solid and Hollow Rectangle Star Pattern Programs
Learn to print solid and hollow rectangle star patterns with programs in C, Python, and Java. Covers loop logic, worked output, and common coding mistakes.
Rectangle star patterns test loop control, conditional logic, and output formatting in a single coding exercise that fits inside a few minutes of reading.
Two variants come up repeatedly in placement coding rounds: solid (every cell is a star) and hollow (only the border is a star). They share the same nested-loop skeleton. Knowing one makes the other trivial.
What Is a Solid Rectangle Star Pattern
A solid rectangle star pattern prints a star in every position of the grid. Given rows r and columns c, the output is r lines each containing c stars.
For input rows = 4, cols = 6:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
There is no conditional logic here. Every cell gets a star. The only job of the inner loop is to print c stars per row, and the outer loop repeats that for r rows.
This pattern is the baseline. If you can write the solid rectangle, you already have the outer-inner loop structure that every other rectangle variant builds on.
Program to Print Solid Rectangle Star Pattern
The programs below take two integer inputs (rows and columns) and print the solid pattern.
In C
#include <stdio.h>
int main() {
int rows, cols, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
The outer loop runs from i = 0 to i < rows. For each value of i, the inner loop runs from j = 0 to j < cols, printing * each time. After the inner loop finishes, printf("\n") moves to the next line. The printf("\n") is outside the inner loop. A common mistake is putting it inside, which would break every row into single-character lines.
In Python
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))
for i in range(rows):
for j in range(cols):
print("*", end=" ")
print()
Python’s range() function generates a sequence from 0 up to (but not including) the argument. range(rows) gives 0, 1, …, rows - 1, which are exactly the row indices you need. The end=" " argument in the inner print() suppresses the default newline and prints a space instead. The bare print() after the inner loop prints the newline at the end of each row.
In Java
import java.util.Scanner;
public class SolidRectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns: ");
int cols = sc.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
In Java, System.out.print("* ") prints without a newline, and System.out.println() prints the newline at the end of each row. The Scanner class reads integer input from the console.
All three programs produce the same output for the same inputs. The loop structure is identical across languages; only the syntax differs.
What Is a Hollow Rectangle Star Pattern
A hollow rectangle star pattern prints stars only on the border of the grid. Interior cells are printed as spaces. The border consists of four sets of cells:
- The first row (all cells in row 0)
- The last row (all cells in row
rows - 1) - The first column (all cells in column 0)
- The last column (all cells in column
cols - 1)
For input rows = 4, cols = 6:
* * * * * *
* *
* *
* * * * * *
Rows 1 and 2 (the middle rows) print a star at position 0 and position 5, with spaces in between. This is the hollow interior.
Note: the original FACE Prep article from 2018 showed the middle row of a 3×5 hollow rectangle as * *, which loses the interior spacing. The correct output for 3 rows, 5 cols is:
* * * * *
* *
* * * * *
Row 1 has a star at column 0 and column 4, with three interior cells (each printed as ) between them.
Program to Print Hollow Rectangle Star Pattern
The key addition over the solid rectangle is one if-else condition inside the inner loop.
In C
#include <stdio.h>
int main() {
int rows, cols, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
The condition i == 0 || i == rows - 1 || j == 0 || j == cols - 1 evaluates to true when the current cell is on any border. If true, print a star. Otherwise, print two spaces to match the width of * .
In Python
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))
for i in range(rows):
for j in range(cols):
if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Python uses or instead of || for logical OR. The rest of the structure mirrors C. The else branch prints a single space followed by end=" ", giving a two-character wide cell that matches the star cell’s width.
For a more complete reference on C pattern variations, GeeksforGeeks maintains a pattern printing reference that covers related shapes.
In Java
import java.util.Scanner;
public class HollowRectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns: ");
int cols = sc.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
The Java version is nearly identical to C. The condition and loop structure are the same; only the I/O calls differ.
Loop Logic: How Both Patterns Work
Both patterns share the same outer-inner loop structure. Understanding this structure means you can adapt it to any rectangle-based pattern.
The outer loop iterates over rows. Each iteration of the outer loop represents one line of output. At the end of each outer-loop iteration, a newline is printed.
The inner loop iterates over columns within the current row. Each iteration of the inner loop represents one cell. The decision of what to print in each cell is made inside the inner loop.
For the solid rectangle, that decision is trivial: always print a star. For the hollow rectangle, the decision depends on the cell’s position.
The time complexity of both patterns is O(rows x cols): every cell is visited exactly once. Space complexity is O(1) because no extra data structure is needed; output is produced directly during iteration.
Once you understand this skeleton, you can extend it. A right-aligned triangle just adds a different starting condition for j. A diamond star pattern combines an upper triangle with a mirrored lower triangle. A palindromic pyramid pattern adds number logic to the same two-loop frame.
The rectangle is the simplest case. It belongs to the same family of nested-loop output problems that appear across coding rounds and lab exercises in B.E. and B.Tech programmes at Tier-2 and Tier-3 colleges across India.
Common Mistakes and Edge Cases
Four mistakes account for most wrong outputs in placement coding submissions:
- Newline inside the inner loop. Placing
printf("\n")orprint()inside the inner loop prints a newline after every single star instead of after every row. The outer loop should own the newline. - Mismatched cell width. If the star cell prints
*(star plus space) but the interior cell prints(one space), the columns will not align. Both cells must print the same total width. - Off-by-one on the border condition. A common error is writing
j == colsinstead ofj == cols - 1for the last column check. If columns are indexed 0 tocols - 1, the last column index iscols - 1, notcols. - Single-row hollow rectangle. When rows equals 1, every cell satisfies both
i == 0(first row) andi == rows - 1(last row). The output is a solid row of stars, which is the correct behaviour for a 1-row hollow rectangle.
Testing with small inputs catches most of these before submission. Try rows = 2 cols = 3, rows = 1 cols = 5, and rows = 3 cols = 1 as your first checks.
If you want to extend your practice to roles that expect more than pattern output, IT jobs for freshers covers which output-formatting and loop skills show up in entry-level screening tests and what proficiency level those tests expect.
What to Build Next
The nested loop logic in both patterns works like this: outer loop for rows, inner loop for columns, a condition inside to decide what to print. That same Python for-loop thinking drives real API work. When you iterate over a list of LLM responses, the outer loop is your request batch and the inner loop parses each token or chunk from the response. TinkerLLM puts actual LLM API calls in your hands for ₹299, with no local setup required. You write Python loops against a real model endpoint and see the output the same way you see the star pattern: one cell at a time, determined by your condition.
Primary sources
Frequently asked questions
What is a hollow rectangle star pattern?
A hollow rectangle star pattern prints stars only on the border: first row, last row, first column, and last column. Interior cells are printed as spaces.
What is the difference between solid and hollow rectangle star patterns?
A solid rectangle fills every cell with a star. A hollow rectangle fills only the border cells with stars and leaves the interior blank.
How do I print a hollow rectangle pattern in Python?
Use nested for loops. In the inner loop, check: if i == 0 or i == rows - 1 or j == 0 or j == cols - 1, print a star; otherwise print a space.
Why does my hollow rectangle pattern show wrong spacing or alignment?
The most common cause is placing the newline inside the inner loop. The newline must come after the inner loop completes, not inside it.
Can I use while loops instead of for loops for rectangle patterns?
Yes. Replace each for loop with a while loop that increments a counter manually. The logic is identical; for loops are preferred because they are more concise.
What does the border condition mean in the hollow rectangle program?
The condition i == 0 or i == rows - 1 or j == 0 or j == cols - 1 evaluates to true for any cell on the first row, last row, first column, or last column: these are the border cells that get a star.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)