Program to Print Square Star Pattern

PRINTING PATTERN:

****

****

****

****

PREREQUISITE:

Basic knowledge of C language and loops.

ALGORITHM:

  1. Take number of rows as input from the user (length of side of the square) and store it in any variable (‘‘ in this case).
  2. Run a loop ‘‘ number of times to iterate through 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 previous loop to iterate through the columns. From j=0 to j<l. The loop should be structured as for(j=0;j<l;j++) .
  4. Print ‘*’ inside the nested loop to print ‘*’s in all the columns of a row.
  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 number of rows
printf("Enter the number of rows/columns\n");  //Asking user for input
scanf("%d",&l);    //Taking the input for number of rows
for(int i=0;i<l;i++)  //Outer loop for number of rows  
   {
      for(int j=0;j<l;j++)   //Inner loop for number of columns in each row
         {
            printf("*");     //Printing '*' in each column of a row.
         }
      printf("\n");    //Printing a new line after each row has been printed.
   }
}

Taking input:

DISPLAYING OUTPUT:

 

3 comments on “Program to Print Square Star Pattern”


  • Lalit Kr.

    Code in JAVA:
    import java.util.*;
    public class Star1 {
    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++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    }
    }


    • HelpPrepInsta

      Its simple charan, we have used 2 loops and for a outer loop the inner loop is traversed to its whole limit.
      That is the same thing which we are doing here, for each value of i, j loop will iterate from start to end
      and hence print * in a square pattern, because we have set the same upper limit of both the loops.
      We hope now you must have got the question