Print Hollow Square Star Pattern

PRINTING 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:

33 comments on “Print Hollow Square Star Pattern”


  • Lalit Kr.

    Code in JAVA:
    import java.util.*;
    public class Star2 {
    public static void main(String arg[])
    {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<n; j++)
    {
    if(i==0 || i==n-1 || j==0 || j==n-1)
    {
    System.out.print("*");
    }
    else
    {
    System.out.print(" ");
    }
    }
    System.out.println();
    }
    }
    }


  • Areefa

    n=int(input())
    for i in range(n):
    if i==0 or i==n-1:
    print(‘*’*n)
    else:
    print(‘*’,end=””)
    for j in range(n-2):
    print(‘ ‘,end=””)
    print(‘*’)
    j=0


  • S-S-H

    PYTHON CODE:
    n = int(input())
    for i in range(n):
    for j in range(n):
    if (i == 0 or i == n-1 or j == 0 or j == n-1):
    print(“*”, end = ” “)
    else:
    print(” “, end = ” “)
    print()


  • RAJ

    #python
    l=int(input())
    for i in range(l):
    if i == 0 or i == l-1:
    print(“*”*l)
    else:
    print(“*”+” “*(l-2)+”*”)


  • Saurabh

    Python 3.8.5 conda interpreter:-
    CODE:-
    def hallowsquarestarpattern(rows,columns):

    for i in range(rows):

    for j in range(columns):

    if i == 0 or i == (rows-1) or j == 0 or j == (columns – 1):

    print(‘*’,end=””)

    else:

    print(” “,end=””)

    print()

    if __name__ == “__main__”:
    rows = int(input(“Enter the number of rows”))
    columns = int(input(“Enter the number of columns”))
    hallowsquarestarpattern(rows ,columns)


  • Notgood

    PYTHON 3
    a=4
    for i in range(4):
    for j in range(4):
    if(i==0 or i==a-1 or j==0 or j==a-1):
    print(“*”,end=” “)
    else:
    print(” “,end=” “)
    print()


  • Sudip

    r=int(input())
    for i in range(0,4):
    if i==1 or i==2:
    for j in range(0,4):
    if j==1 or j==2:
    print(‘ ‘,end=”)
    elif j==3:
    print(‘*’,end=’\n’)
    else:
    print(‘*’,end=”)
    else:
    print(“*”*4)


  • 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)*’ ‘ + ‘*’)


  • 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)


  • 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;
    }


  • 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)


      • Lakshmi

        n=int(input())
        for i in range(1,n+1):
        if i == 1 or i ==n:
        print(n*”*”)
        else:
        print(“*”+ (n-2)*” ” + “*”)


      • sayannandi11071998

        #For Python User
        n = 6
        for i in range(1, n+1):
        for j in range(1, n+1):
        if (i == 1 or i == n or j == 1 or j == n):
        print(“*”, end=””)
        else:
        print(” “, end=””)
        print()