Loop Assignment 22 — Print a Plus-Sign Star Pattern Using Nested For Loops
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Print a Plus-Sign Star Pattern Using Nested For Loops — Middle Row and Middle Column Pattern

📑 On this page:
  • How the Plus-Sign Pattern Works
  • The Three Steps of Printing the Pattern
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • 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 == mid and j == 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.

Diagram showing a plus-sign star pattern created using nested for loops in C

The Three Steps of Printing the Pattern

Step Loop operation Effect
1. Find the middle positionmid = (rows + 1) / 2;Identifies the center row and center column
2. Visit every positionfor (i = 1; i <= rows; i++) and for (j = 1; j <= rows; j++)Checks every row and column in the square
3. Print star or spaceif (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 == mid is 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 == mid is true, so print *.
  • Position (3,2): i == mid is true, so print *.
  • Position (3,3): both conditions are true, so print *.
  • Position (3,4): i == mid is true, so print *.
  • Position (3,5): i == mid is 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.

📖 Related Tutorials

  • 21. Print an X-Shaped Star Pattern Using Nested For Loops
  • 23. Print a Number Palindrome Pyramid Using Nested Loops
  • 9. Print the Pattern of a Pyramid Using a For Loop
  • 13. Print the Pattern of a Diamond Using a For Loop
  • All Loop Assignments in C

Previous Topic: -->> 21. Write a program in C to print an X-shaped star pattern using nested for loops.   ||   Next Topic: -->> 23. Write a program in C to print a number palindrome pyramid (e.g., 1, 121, 12321) using nested loops.


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.