Java Program to Print Star Pattern, Pattern of Stars (*)

This article is created to cover a lot of pattern programs using star (*) in Java. This article covers all the famous pattern programs using star. More than 15 pattern programs using star, are included in this article.

Print Star Pattern in Java - Pattern No.1

The question is, write a Java program to print star pattern. The program given below is its answer:

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j;
      for(i=0; i<5; i++)
      {
         for(j=0; j<=i; j++)
         {
            System.out.print("* ");
         }
         System.out.print("\n");
      }
   }
}

The snapshot given below shows the sample output produced by above program in Java, on printing of star (*) pattern:

java print star pattern no 1

The dry run of above program goes like:

The above program can also be created as:

public class CodesCracker
{
   public static void main(String[] args)
   {
      for(int i=0; i<5; i++)
      {
         for(int j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

You'll get the exact output as of previous program. Since the program given above, prints pattern of stars with limited number of lines or rows. Therefore let's modify the program and create a new one that allows user to define the row size of star pattern:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row, i, j;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the Number of Lines (Row): ");
      row = s.nextInt();
      
      for(i=0; i<row; i++)
      {
         for(j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

The sample run of above program with user input 10 as number of lines/rows to expand the pattern, is given in the following snapshot:

java print pattern of stars

You can use the same, to allow user to define the size of row/line to expand the pattern, in any program given here.

Print Star Pattern in Java - Pattern No.2

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row=5, i, j, space, spaceLimit;
      spaceLimit = (row*2) - 2;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
         spaceLimit = spaceLimit-2;
      }
   }
}

The output of above program is:

        * 
      * * 
    * * * 
  * * * * 
* * * * * 

Note - To increase the size, just increase the value of row from 5 to more say 8, 10, 11 etc.

Also the above program can also be created in this way, to allow user to define the size of the pattern:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row, i, j, space, spaceLimit;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the Row Size of Pattern: ");
      row = s.nextInt();
      
      spaceLimit = (row*2) - 2;
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
         spaceLimit = spaceLimit-2;
      }
   }
}

The sample run of above program with user input 15 is shown in the snapshot given below:

print star pattern in java

You can apply the same in other programs too, to print the specified size of pattern by user at run-time of the program.

Print Star Pattern in Java - Pattern No.3

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row=5, i, j;
      
      for(i=0; i<row; i++)
      {
         for(j=i; j<row; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

Output of Pattern No.3 Program

* * * * * 
* * * * 
* * * 
* * 
* 

Print Star Pattern in Java - Pattern No.4

public class CodesCracker
{
   public static void main(String[] args)
   {
      int row=5, i, j, space, spaceLimit=0;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=i; j<row; j++)
            System.out.print("* ");
         System.out.print("\n");
         spaceLimit = spaceLimit + 2;
      }
   }
}

Output of Pattern No.4 Program

* * * * * 
  * * * * 
    * * * 
      * * 
        * 

Print Star Pattern in Java - Pattern No.5

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10;
      for(i=0; i<row; i++)
      {
         for(j=0; j<=i; j++)
         {
            if(i==0 || i==1)
               System.out.print("* ");
            else if(i==(row-1))
               System.out.print("* ");
            else
            {
               if(j==0 || j==i)
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
      }
   }
}

Output of Pattern No.5 Program

* 
* * 
*   * 
*     * 
*       * 
*         * 
*           * 
*             * 
*               * 
* * * * * * * * * * 

Print Star Pattern in Java - Pattern No.6

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space, spaceLimit;
      spaceLimit = (row*2) - 2;
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
         {
            if(i==0 || i==1)
               System.out.print("* ");
            else if(i==(row-1))
               System.out.print("* ");
            else
            {
               if(j==0 || j==i)
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
         spaceLimit = spaceLimit - 2;
      }
   }
}

Output of Pattern No.6 Program

                  * 
                * * 
              *   * 
            *     * 
          *       * 
        *         * 
      *           * 
    *             * 
  *               * 
* * * * * * * * * * 

Print Star Pattern in Java - Pattern No.7

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10;
      for(i=0; i<row; i++)
      {
         for(j=i; j<row; j++)
         {
            if(i==0 || i==(row-1) || i==(row-2))
               System.out.print("* ");
            else
            {
               if(j==i || j==(row-1))
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
      }
   }
}

Output of Pattern No.7 Program

* * * * * * * * * * 
*               * 
*             * 
*           * 
*         * 
*       * 
*     * 
*   * 
* * 
* 

Print Star Pattern in Java - Pattern No.8

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space, spaceLimit=0;
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=i; j<row; j++)
         {
            if(i==0 || i==(row-1) || i==(row-2))
               System.out.print("* ");
            else
            {
               if(j==i || j==(row-1))
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
         spaceLimit += 2;
      }
   }
}

Output of Pattern No.8 Program

* * * * * * * * * * 
  *               * 
    *             * 
      *           * 
        *         * 
          *       * 
            *     * 
              *   * 
                * * 
                  * 

Print Star Pattern in Java - Pattern No.9

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space;
      
      for(i=0; i<row; i++)
      {
         for(space=i; space<row; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

Output of Pattern No.9 Program

          * 
         * * 
        * * * 
       * * * * 
      * * * * * 
     * * * * * * 
    * * * * * * * 
   * * * * * * * * 
  * * * * * * * * * 
 * * * * * * * * * * 

Print Star Pattern in Java - Pattern No.10

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space;
      
      for(i=0; i<row; i++)
      {
         for(space=i; space<row; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
         {
            if(i==0 || i==1)
               System.out.print("* ");
            else if(i==(row-1))
               System.out.print("* ");
            else
            {
               if(j==0 || j==i)
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
      }
   }
}

Output of Pattern No.10 Program

          * 
         * * 
        *   * 
       *     * 
      *       * 
     *         * 
    *           * 
   *             * 
  *               * 
 * * * * * * * * * *

Print Star Pattern in Java - Pattern No.11

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<i; space++)
            System.out.print(" ");
         for(j=i; j<row; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

Output of Pattern No.11 Program

* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         * 

Print Star Pattern in Java - Pattern No.12

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<i; space++)
            System.out.print(" ");
         for(j=i; j<row; j++)
         {
            if(i==0 || i==(row-1) || i==(row-2))
               System.out.print("* ");
            else
            {
               if(j==i || j==(row-1))
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
      }
   }
}

Output of Pattern No.12 Program

* * * * * * * * * * 
 *               * 
  *             * 
   *           * 
    *         * 
     *       * 
      *     * 
       *   * 
        * * 
         * 

Print Star Pattern in Java - Pattern No.13

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=7;
      for(i=0; i<row; i++)
      {
         for(j=i; j>=0; j--)
            System.out.print("* ");
         System.out.print("\n");
      }
      for(i=0; i<(row-1); i++)
      {
         for(j=(row-1); j>i; j--)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

Output of Pattern No.13 Program

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Above program can also be written as following program. This program shows the actual use of row. That is, in above program, the value of row is given 7, but the pattern expanded upto 13 lines. Therefore, we need to modify that program:

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=13, decider;
      if(row%2==0)
         decider = row/2;
      else
         decider = (row/2) + 1;
      
      for(i=0; i<row; i++)
      {
         if(i<decider)
         {
            for(j=i; j>=0; j--)
               System.out.print("* ");
         }
         else
         {
            for(j=i; j<row; j++)
               System.out.print("* ");
         }
         System.out.print("\n");
      }
   }
}

It will produces the same output as shown in the snapshot given below:

java print different star pattern

Note - You can do a lot of modification in any of the program given here, to improve your concept. Because, the same program can be created in multiple ways.

Print Star Pattern in Java - Pattern No.14

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=13, decider;
      if(row%2==0)
         decider = row/2;
      else
         decider = (row/2) + 1;
      
      for(i=0; i<row; i++)
      {
         if(i<decider)
         {
            for(j=i; j>=0; j--)
            {
               if(i==0 || i==1)
                  System.out.print("* ");
               else if(j==i || j==0)
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         else
         {
            for(j=i; j<row; j++)
            {
               if(i==(row-1) || i==(row-2))
                  System.out.print("* ");
               else if(j==i || j==(row-1))
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
      }
   }
}

Output of Pattern No.14 Program

* 
* * 
*   * 
*     * 
*       * 
*         * 
*           * 
*         * 
*       * 
*     * 
*   * 
* * 
* 

Print Star Pattern in Java - Pattern No.15

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=7, space, spaceLimit;
      spaceLimit = (row*2) - 2;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
         spaceLimit -= 2;
      }
      spaceLimit = 0;
      for(i=0; i<(row-1); i++)
      {
         for(space=i; space<=spaceLimit; space++)
            System.out.print("  ");
         for(j=(row-1); j>i; j--)
            System.out.print("* ");
         System.out.print("\n");
         spaceLimit += 2;
      }
   }
}

Output of Pattern No.15 Program

            * 
          * * 
        * * * 
      * * * * 
    * * * * * 
  * * * * * * 
* * * * * * * 
  * * * * * * 
    * * * * * 
      * * * * 
        * * * 
          * * 
            * 

Print Star Pattern in Java - Pattern No.16

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=7, space, spaceLimit;
      spaceLimit = (row*2) - 2;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<spaceLimit; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
         {
            if(i==0 || i==1)
               System.out.print("* ");
            else
            {
               if(j==0 || j==i)
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
         spaceLimit -= 2;
      }
      spaceLimit = 0;
      for(i=0; i<(row-1); i++)
      {
         for(space=i; space<=spaceLimit; space++)
            System.out.print("  ");
         for(j=(row-1); j>i; j--)
         {
            if(i==(row-2) || i==(row-3))
               System.out.print("* ");
            else
            {
               if(j==(row-1) || j==(i+1))
                  System.out.print("* ");
               else
                  System.out.print("  ");
            }
         }
         System.out.print("\n");
         spaceLimit += 2;
      }
   }
}

Output of Pattern No.16 Program

            * 
          * * 
        *   * 
      *     * 
    *       * 
  *         * 
*           * 
  *         * 
    *       * 
      *     * 
        *   * 
          * * 
            * 

Print Star Pattern in Java - Pattern No.17

From any pattern given above, you can also combine any two to create another pattern, like shown in the program and its sample output given below:

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, row=10, space;
      
      for(i=0; i<row; i++)
      {
         for(space=0; space<i; space++)
            System.out.print(" ");
         for(j=i; j<row; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
      for(i=0; i<row; i++)
      {
         for(space=(i+1); space<row; space++)
            System.out.print(" ");
         for(j=0; j<=i; j++)
            System.out.print("* ");
         System.out.print("\n");
      }
   }
}

Output of Pattern No.17 Program

java pattern program of stars

Print Star Pattern in Java - Pattern No.18

Here is another and last pattern of stars, I'm going to create for this article.

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, j, k=1, row=5;
      for(i=0; i<row; i++)
      {
         for(j=0; j<k; j++)
            System.out.print("* ");
         
         k += 2;
         System.out.print("\n");
      }
   }
}

Output of Pattern No.18 Program

* 
* * * 
* * * * * 
* * * * * * * 
* * * * * * * * * 

Note - There are tons of pattern you can try with. These programs are just demo, to show you, how the code can be written in Java, to design different - different star pattern. Still you can design the pattern in your mind and implement that pattern in programming world. Do it yourself. For sure, it will boost your logic.

Java Online Test


« Previous Program Next Program »


Liked this post? Share it!