C Program to Print Hollow Square Pattern With Diagonals

Write a C Program to Print Hollow Square Star Pattern With Diagonals using for loop. This c example uses the nested for loops to iterate rows and columns and if else to check for diagonals to print Hollow Square star Pattern With Diagonals.

#include<stdio.h>
int main()
{
    int i, j, rows;
    printf("Enter Hollow Square with Diagonals Rows =  ");
    scanf("%d", &rows);

    printf("Hollow Square Star Pattern With Diagonals\n");
    for(i = 1; i <= rows; i++)
    {
        for(j = 1; j <= rows; j++)
        {
            if(i == 1 || i == rows || i == j || j == 1 || j == rows || j == rows - i + 1)
            {
                printf("* ");
            }
            else
            {
                printf("  ");
            }         
        }
        printf("\n");   
    }
    return 0;
}
C Program to Print Hollow Square Pattern With Diagonals 1

This C Program allows entering symbols to print the Hollow Square Pattern With Diagonals using a while loop.

#include<stdio.h>
int main()
{
    int i, j, rows;
    char ch;
    
    printf("Symbol for Hollow Square with Diagonals =  ");
    scanf("%c", &ch);

    printf("Enter Hollow Square with Diagonals Rows =  ");
    scanf("%d", &rows);

    printf("Hollow Square Pattern With Diagonals\n");
    i = 1;
    while(i <= rows)
    {
        j = 1;
        while(j <= rows)
        {
            if(i == 1 || i == rows || i == j || j == 1 || j == rows || j == rows - i + 1)
            {
                printf("%c ", ch);
            }
            else
            {
                printf("  ");
            } 
            j++;        
        }
        printf("\n");  
        i++; 
    }
    return 0;
}
Symbol for Hollow Square with Diagonals =  #
Enter Hollow Square with Diagonals Rows =  12
Hollow Square Pattern With Diagonals
# # # # # # # # # # # # 
# #                 # # 
#   #             #   # 
#     #         #     # 
#       #     #       # 
#         # #         # 
#         # #         # 
#       #     #       # 
#     #         #     # 
#   #             #   # 
# #                 # # 
# # # # # # # # # # # # 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.