C program to print hollow square or rectangle star pattern.
*****
* *
* *
* *
*****
C Programming Language / Loop control in C Language
1337Program:
/** * C program to print hollow square star pattern */ #include 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++) { if(i==1 || i==N || j==1 || j==N) { /* Print star for 1st, Nth row and column */ printf("*"); } else { printf(" "); } } /* Move to the next line/row */ printf("\n"); } return 0; }
Output:
Enter number of rows: 5 ***** * * * * * * *****
Explanation:
Logic to print hollow square star pattern
***** * * * * * * *****
The above pattern is similar to square star pattern of N rows and N columns. Here star is printed only for first and last column or for first and last row.
Step by step descriptive logic to print empty square star pattern.
- Input number of rows to print from user. Store it in a variable say N.
- To iterate through rows, run an outer loop from 1 to N. For that define loop with structure
for(i=1; i<=N; i++)
. - To iterate through columns, run an inner loop from 1 to N. Define loop with structure
for(j=1; j<=N; j++)
. - Inside inner loop print star for first and last row or for first and last column. Which is print star if
i==1
ori==N
orj==1
orj==N
, otherwise print space. - After printing all columns of a row, move to next line i.e. print a blank line after inner loop.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS