8 Different Star(*) Pattern Programs in Java

In this article, we will learn, different star pattern programs in Java.

java star pattern
 

Star Pattern Example 1:


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

package com.topjavatutorial;

public class JavaStarPattern {

  public static void main(String[] args) {
    for (int row = 8; row >= 1; --row) {
      for (int col = 1; col <= row; ++col) {
        System.out.print("*");
      }

      System.out.println();
    }
  }
}

 

Star Pattern Example 2:


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

package com.topjavatutorial;

public class JavaStarPattern2 {

  public static void main(String[] args) {
    for (int row = 1; row <= 8; ++row) {
      for (int col = 1; col <= row; ++col) {
        System.out.print("*");
      }

      System.out.println();
    }
  }

}


 

Star Pattern Example 3 :


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

package com.topjavatutorial;

public class JavaStarPattern {

  public static void main(String[] args) {
    int number = 7;
    int count = number - 1;
    for (int k = 1; k <= number; k++) {
      for (int i = 1; i <= count; i++)
        System.out.print(" ");
      count--;
      for (int i = 1; i <= 2 * k - 1; i++)
        System.out.print("*");
      System.out.println();

    }
  }
}

 

Star Pattern Example 4: Diamond Pattern


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

package com.topjavatutorial;

public class JavaStarPattern {

  public static void main(String[] args) {
    int number = 5;
    int count = number - 1;
    for (int k = 1; k <= number; k++) {
      for (int i = 1; i <= count; i++)
        System.out.print(" ");
      count--;
      for (int i = 1; i <= 2 * k - 1; i++)
        System.out.print("*");
      System.out.println();
    }
    count = 1;
    for (int k = 1; k <= number - 1; k++) {
      for (int i = 1; i <= count; i++)
        System.out.print(" ");
      count++;
      for (int i = 1; i <= 2 * (number - k) - 1; i++)
        System.out.print("*");
      System.out.println();
    }
  }
}

 

Star Pattern Example 5:


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

package com.topjavatutorial;

public class JavaStarPattern {

  public static void main(String[] args) {
    int val = 8;
    for (int i = 1; i <= val; i++) {
      for (int j = 1; j <= val - i; j++) {
        System.out.print(" ");
      }
      for (int k = 1; k <= i; k++) {
        System.out.print("*");
      }
      System.out.println("");
    }
  }

}


 

Star Pattern Example 6:


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

package com.topjavatutorial;

public class JavaStarPattern {

  public static void main(String[] args) {
    int number = 8;

    for (int i = 0; i < number; ++i) {
      for (int j = 0; j <= i; ++j) {
        System.out.print("*");
      }

      if (i != number - 1) {
        System.out.print(" ");
      } else {
        System.out.print(" *");
      }
      for (int j = 0; j <= i; ++j) {
        System.out.print("*");
      }

      System.out.println();
    }
  }
}


 

Star Pattern Example 7:


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

package com.topjavatutorial;

public class JavaStarPattern7 {

  public static void main(String[] args) {
    int number = 7;

    for (int i = 0; i < number; i++) {
      if (i == 0 || i == 6) {
        for (int j = 0; j < number; j++) {
          System.out.print("*");
        }
        System.out.println();
      }
      if (i >= 1 && i <= 5) {
        for (int j = 0; j < number; j++) {
          if (j == 0 || j == 6) {
            System.out.print("*");
          } else if (j >= 1 && j <= 5) {
            System.out.print(" ");
          }
        }
        System.out.println();
      }
    }
  }
}

 

Star Pattern Example 8:


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

package com.topjavatutorial;

public class JavaStarPattern {

  private static void stars(int count) {
    for (int i = 0; i < count; ++i)
      System.out.print("*");
  }

  private static void spaces(int count) {
    for (int i = 0; i < count; ++i)
      System.out.print(" ");
  }

  public static void main(String[] args) {

    int n = 8;

    for (int i = 0; i < n; ++i) {
      stars(i + 1);
      spaces(n - i - 1);
      stars(n - i + 1);
      spaces(2 * i);
      stars(n - i);
      spaces(n - i - 1);
      stars(i + 1);

      System.out.println();
    }

  }
}

 
Thanks for visiting !!
 

You may also like

 

© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

3 comments

  1. in pattern 6
    isn’t there an extra star in the last line?
    should it be there?

  2. […] 8 Different Star(*) Pattern Programs in Java […]

  3. for program no 6 if else condition not required .it can be replace with simple system.out.print(” “);

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: