CodesJava

Easy learning with example program codes

java star pattern programs


Pattern Example-1

Pattern

********
*******
******
*****
****
***
**
*
package com.codesjava;
public class Test {
 
    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();
        }
    }
}

Output

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

Pattern Example-2

Pattern

*
**
***
****
*****
******
*******
********
package com.codesjava;
public class Test {	 
    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();
        }
    }
 
}

Output

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

Pattern Example-3

Pattern

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

package com.codesjava;
public class Test {
 
    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();
 
        }
    }
}

Output

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

Pattern Example-4

Pattern

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

package com.codesjava;
public class Test {
 
    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();
        }
    }
}

Output

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

Pattern Example-5

Pattern

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

package com.codesjava;
public class Test {	 
    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("");
        }
    }
 
}

Output

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

Pattern Example-6

Pattern

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

package com.codesjava;
public class Test {	 
    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();
        }
    }
}

Output

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

Pattern Example-7

Pattern

*******
*     *
*     *
*     *
*     *
*     *
*******
package com.codesjava;
public class Test {	 
    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();
            }
        }
    }
}

Output

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

Pattern Example-8

Pattern

*       *****************       *
**      ********  *******      **
***     *******    ******     ***
****    ******      *****    ****
*****   *****        ****   *****
******  ****          ***  ******
******* ***            ** *******
**********              *********
package com.codesjava;
public class Test {	 
    private static void forStars(int count) {
        for (int i = 0; i < count; ++i)
            System.out.print("*");
    }
 
    private static void forSpaces(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) {
        	forStars(i + 1);
            forSpaces(n - i - 1);
            forStars(n - i + 1);
            forSpaces(2 * i);
            forStars(n - i);
            forSpaces(n - i - 1);
            forStars(i + 1);
 
            System.out.println();
        }
 
    }
}

Output

*       *****************       *
**      ********  *******      **
***     *******    ******     ***
****    ******      *****    ****
*****   *****        ****   *****
******  ****          ***  ******
******* ***            ** *******
**********              *********
Please follow and like us:
Posted in Java   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki