Home C Programming Language / Programs / C program to print hollow square star pattern with diagonal ***** ** ** * * * ** ** *****
🚀 Programming Example

C program to print hollow square star pattern with diagonal
*****
** **
* * *
** **
*****

👁 2,912 Views
💻 Practical Program
📘 Step Learning
Write a C program to print hollow square star pattern with diagonal using loops. How to print hollow square star pattern with diagonals in C program. Logic to print hollow square star pattern with diagonal in C programming.

💻 Program Code

/**
 * C program to print hollow square star pattern with diagonal
* Programmer: atnyla
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    /* Input number of rows from user */
    printf("Enter number of rows: ");
    scanf("%d", &N);

    /* Iterate over each row */
    for(i=1; i<=N; i++)
    {
        /* Iterate over each column */
        for(j=1; j<=N; j++)
        {
            /*
             * Print star for, 
             * first row (i==1) or 
             * last row (i==N) or
             * first column (j==1) or
             * last column (j==N) or 
             * row equal to column (i==j) or 
             * column equal to N-row (j==N-i+1)
             */
            if(i==1 || i==N || j==1 || j==N || i==j || j==(N - i + 1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        /* Move to the next line */
        printf("\n");
    }

    return 0;
}
                        

🖥 Program Output

<pre>
Enter number of rows: 5 
*****
** **
* * *
** **
*****
</pre>
                            

📘 Explanation

Required knowledge

Basic C programming, If else, Logical operator, For loop, Nested loop

Logic to print hollow square with diagonal

 

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

 

The above pattern is a simple hollow square star pattern if we remove diagonal. It consists of N rows and columns (where N is the number of rows to print). For each row stars are printed in four conditions.

  • For first or last row.
  • For first or last columns.
  • If row equals to column or row equals to N-i+1 (where i is current row number).

Step by step descriptive logic to print hollow square star pattern with diagonal.

  1. Input number of rows to print from user. Store it in some variable say N.
  2. To iterate through rows run an outer loop from 1 to N, increment loop counter by 1. The loop structure should look like for(i=1; i<=N; i++).
  3. To iterate through columns run an inner loop from 1 to N, increment loop counter by 1. The loop structure should look like for(j=1; j<=N; j++).
  4. Inside the loop print star for first or last row, first or last column or when row equals to column or row equals to N - i + 1 otherwise print space.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.