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”


  • Mohit

    for i in range(4):
    for j in range(4):
    if (i== 1 or i ==2) and (j==2 or j==1):
    print(‘ ‘,end= ”)
    else:
    print(‘*’,end= ”)
    print()


  • tondalevishal143

    n=int(input())
    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()


  • ELAKKIYA

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


  • BASKARASETHUPATHY

    “””for python”””
    n = int(input(“please give input for no. of stars”))
    def m():
    print(“”)
    for i in range(1, n+1):
    if i == 1 or i == n: print(f”{‘*’* n}”)
    else:
    for _ in range(1, n+1):
    if _ ==1 or _ == n:
    print(“*”, end=””)
    if _==n:
    m()
    else:
    print(” “, end=””)


  • Gowtham

    n=int(input())
    for i in range(n):
    if(i!=n-1 and i!=0):
    print(“*”,end=””)

    print(” “*(n-2),end=””)
    print(“*”,end=””)
    else:
    print(“*”*n,end=””)
    print()


  • 19-309-

    n = int(input())
    s = list(n * ‘ ‘)
    s[0],s[n-1] = ‘*’, ‘*’
    for i in range(n):
    if i == 0 or i == n-1:
    print(n * ‘*’)

    else:
    print(“”.join(s))


  • gautam

    “””for python”””
    n = int(input(“please give input for no. of stars”))
    def m():
    print(“”)
    for i in range(1, n+1):
    if i == 1 or i == n:

    print(f”{‘*’* n}”)
    else:
    for _ in range(1, n+1):
    if _ ==1 or _ == n:
    print(“*”, end=””)
    if _==n:
    m()
    else:
    print(” “, end=””)


  • Shivam

    #include

    using namespace std;

    int main()
    {
    int n;
    cin>>n;
    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)
    cout<<"*";
    else
    cout<<" ";
    }
    cout<<endl;
    }

    return 0;
    }


  • udhayakumar2352000

    #include
    int main()
    {
    int n,l=1,j;
    scanf(“%d”,&n);
    for(int i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    if(i==l && (j==1 || j==2))
    {
    printf(" ");
    }
    else
    {
    printf("*");
    }
    }
    printf("\n");
    if(i==l && j == n)
    {
    if(l<2)
    {
    l++;
    }
    }
    }
    }


  • kavanamgowda15092000

    #include
    int main()
    {

    int n;
    scanf(“%d”,&n);
    for(int i=0;i<=n;i++)
    {
    for(int j=0;j<=n;j++)
    {
    if(i==0||j==0||i==n||j==n)
    {

    printf("*");
    }
    else
    printf(" ");
    }
    printf("\n");
    }
    }


  • sayandip

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


  • sayandip

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

    for i in range(n):
    print(“*”,end=””)