- How to print a plus-sign star pattern using nested for loops
- How to find the middle row and middle column
- How to print stars using
i == midandj == mid - How to print spaces in all other positions
- Why an odd number of rows is required for a proper plus shape
- How nested loops represent a square matrix
How the Plus-Sign Pattern Works
A plus-sign star pattern is created by printing stars in the middle row and the middle column of a square. All other positions are filled with spaces.
The horizontal line of the plus sign is formed by the middle row, while the vertical line is formed by the middle column. When these two lines intersect, they create the center of the plus sign.
The program uses nested for loops to visit every position in a square of size rows × rows. If the current row or column is the middle position, it prints a star; otherwise, it prints a space.
💡 In one line: Print * when i == mid or j == mid; otherwise print a space.
The Three Steps of Printing the Pattern
| Step | Loop operation | Effect |
|---|---|---|
| 1. Find the middle position | mid = (rows + 1) / 2; | Identifies the center row and center column |
| 2. Visit every position | for (i = 1; i <= rows; i++) and for (j = 1; j <= rows; j++) | Checks every row and column in the square |
| 3. Print star or space | if (i == mid || j == mid) | Prints stars for the plus shape and spaces elsewhere |
The number of rows must be odd, such as 5, 7, or 9. This ensures that there is exactly one middle row and one middle column, allowing the plus sign to have a clear center.
For example, in a 5 × 5 pattern, the middle row is row 3 and the middle column is column 3. Stars are printed in row 3 and column 3, while all other positions contain spaces.
Complete C Program
Program: print a plus-sign star pattern using nested for loops
#include <stdio.h>
int main(void) {
int rows, i, j, mid;
printf("Enter an odd number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0 || rows % 2 == 0) {
printf("Invalid input. Please enter a positive odd number.\n");
return 1;
}
mid = (rows + 1) / 2;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++) {
if (i == mid || j == mid) {
printf("*");
}
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Program Output
Enter an odd number of rows: 5
*
*
*****
*
*
A second run, with 7 rows:
Enter an odd number of rows: 7
*
*
*
*******
*
*
*
The stars form one horizontal line through the middle row and one vertical line through the middle column. Their intersection creates the center of the plus sign.
Step-by-Step Explanation
Let us understand how the program prints a plus-sign pattern for 5 rows.
1. Finding the middle position
For 5 rows, the middle position is calculated as mid = (5 + 1) / 2, which gives 3. Therefore, row 3 is the horizontal line and column 3 is the vertical line.
2. The outer loop controls rows
The outer loop for (i = 1; i <= rows; i++) runs from row 1 to row 5. Each iteration prints one complete row of the pattern.
3. The inner loop controls columns
The inner loop for (j = 1; j <= rows; j++) runs from column 1 to column 5. Together, both loops visit all 25 positions in the 5 × 5 square.
4. Printing the horizontal line
When i == mid, the current row is the middle row. Therefore, the program prints a star in every column of that row, creating the horizontal line of the plus sign.
5. Printing the vertical line
When j == mid, the current column is the middle column. Therefore, the program prints a star in every row of that column, creating the vertical line of the plus sign.
6. Printing spaces
If the current position is neither in the middle row nor in the middle column, the program prints a space. These spaces create the empty areas around the plus sign.
7. Example trace for row 1
- Position (1,1): neither condition is true, so print a space.
- Position (1,2): neither condition is true, so print a space.
- Position (1,3):
j == midis true, so print*. - Position (1,4): neither condition is true, so print a space.
- Position (1,5): neither condition is true, so print a space.
- Row 1 output:
*.
8. Example trace for row 3
- Position (3,1):
i == midis true, so print*. - Position (3,2):
i == midis true, so print*. - Position (3,3): both conditions are true, so print
*. - Position (3,4):
i == midis true, so print*. - Position (3,5):
i == midis true, so print*. - Row 3 output:
*****.
9. Why odd rows are required
An odd number of rows gives the pattern one exact middle row and one exact middle column. With an even number of rows, there is no single center position, so the plus sign does not look properly balanced.
10. Complexity summary
Time complexity is O(n2), because the program visits every position in an n × n square. Space complexity is O(1), because the program uses only loop variables and does not store the pattern in an array.
🎯 Remember this: To print a plus-sign pattern, find the middle position using (rows + 1) / 2, then print a star when i == mid or j == mid. Print spaces everywhere else.
Frequently Asked Questions
1. Why is the middle row important?
The middle row forms the horizontal line of the plus sign. When i == mid, stars are printed across all columns in that row.
2. Why is the middle column important?
The middle column forms the vertical line of the plus sign. When j == mid, stars are printed down all rows in that column.
3. How is the middle position calculated?
When loops start from 1, the middle position is calculated as (rows + 1) / 2. For 5 rows, the middle position is 3.
4. Can this pattern be printed with an even number of rows?
Technically yes, but the plus sign will not have a single exact center. For a clean and symmetrical plus-sign pattern, use an odd number of rows.
5. How is this different from an X-shaped pattern?
An X-shaped pattern prints stars on two diagonals, while a plus-sign pattern prints stars on the middle row and middle column. Both patterns use nested loops and conditional printing.
6. What is the time complexity of this program?
The time complexity is O(n2), because the program visits every position in an n × n square. Space complexity is O(1), since no additional array is used.
💡 Tip: After implementing this program, try printing a hollow square, an X pattern, or a diamond pattern. These patterns help you practice row-column conditions in nested loops.