Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Print Hollow Square Star Pattern

****

*    *

*    *

****

## PREREQUISITE:

Basic knowledge of C language and use of loops.

## ALGORITHM:

1. Take number of rows/columns as input from the user (length of side of square) and store it in any variable (‘l’ in this case).
2. Run  a loop ‘l’ number of times to iterate through all the rows. From i=0 to i<l. The loop should be structured as for( i=0; i<l ; i++).
3. Run a nested loop inside the main loop for printing stars . From j=0 to j<l. The loop should be structured as for( j=0 ; j<l : j++).
4. Inside the above loop print stars only if  i=0 or i=l-1 or j=0 or j=l-1 in all other cases print a blank space.
5. Move to the next line by printing a new line. printf(“\n”).

## Code in C:

```#include<stdio.h>
int main()
{
int i,j,l;    //declaring integers i,j for loops and l for the number of rows
printf(" Enter the number of rows\n");   //Asking user for input
scanf("%d",&l);   //taking input for number of rows and saving in variable l
for(i=0;i<l;i++) //Outer loop for number of rows
{
for(j=0;j<l;j++) //Inner loop for printing stars in each column of a row
{
if(i==0 || i==l-1 || j==0 || j==l-1) // condition for printing stars
{
printf("*");   // printing stars
}
else                 // else condition to print spaces
{
printf(" ");   //printing spaces
}
}
printf("\n");       //Printing a new line after a row has been printed
}
}```

## TAKING INPUT:  DISPLAYING OUTPUT:  ### 6 comments on “Print Hollow Square Star Pattern”

• Krishan

Code in Python3:
def pattern(n): #here, n = size of the box
for i in range(1,n+1):
if i == 1 or i ==n:
print(n*’*’)
else:
print(‘*’+ (n-2)*’ ‘ + ‘*’) 2
• Pooja

def print_rectangle(n, m) :

for i in range(1, n+1) :
for j in range(1, m+1) :
if (i == 1 or i == n or
j == 1 or j == m) :
print(“*”, end=””)
else :
print(” “, end=””)

print()

# Driver program for above function
rows = 6
columns = 20
print_rectangle(rows, columns) 1
• Ramya r

#include

int main() {
// Write C code here
int i,j,n;
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1||i==n)
printf("*");
else
if(j==1||j==n)
printf("* ");
}
printf("\n");
}

return 0;
} 1
• shreyank

****

* *

* *

**** 1
• shreyank

# Function for Hollow Rhombus

def hollowRhombus(rows):

for i in range (1, rows + 1):
# Print trailing spaces
print (end=”\n”)

for j in range (1, rows + 1):
print (end=””)

# Print stars after spaces
# Print stars for each solid rows

if i == 1 or i == rows:
for j in range (1, rows + 1):
print (“*”,end=””)

# stars for hollow rows
else:
for j in range (1,rows+1):
if (j == 1 or j == rows):
print (“* “,end=””)
else:
print (end=” “)
# Move to the next line/row
print()

# utility program to print all patterns
def printPattern(rows):
hollowRhombus(rows)

# driver program

if __name__ == “__main__”:

rows = 4
printPattern (rows) 1
• HelpPrepInsta

Thanks for contributing the code Shreyank 0