DEV Community

Guna Sekaran
Guna Sekaran

Posted on

1

Java Programs for Number Pattern Printing

private static void pattern1() {
        for(int row=1;row<=9;row++)
        {
            for(int col=1;col<=9;col++) {

                if(col==5 || row==9 ||col+row==6 ) {
                    System.out.print("* ");
                    }
                else {
                    System.out.print("  ");
                }
                }

                System.out.println();   

        }   
    }
    }
.........................................................................

OUTPUT:
        *         
      * *         
    *   *         
  *     *         
*       *         
        *         
        *         
        *         
* * * * * * * * * 

Enter fullscreen mode Exit fullscreen mode
private static void pattern2() {
        for(int row=1;row<=9;row++) {
            for(int col=1;col<=9;col++) {
                if(row==1|| col==9&&row<=5||row==5||row==9||col==1&&row>=5) {
                    System.out.print("* ");
                }
                else {
                    System.out.print("  ");
                    }
            }
            System.out.println();
        }

    }

.........................................................................

OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern3() {
        for(int row=1;row<=9;row++) {
            for(int col=1;col<=9;col++) {
                if(row==1|| row==9|| col==9|| row==5) {
                    System.out.print("* ");
                }
                else {
                    System.out.print("  ");
                    }
            }
            System.out.println();
        }

    }

.........................................................................

OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern4() {
        for(int row=1;row<=9;row++) {
            for(int col=1;col<=9;col++) {
                if(col==1&&row<=6|| row==6|| col==5&&row>=3) {
                    System.out.print("* ");
                }
                else {
                    System.out.print("  ");
                    }
            }
            System.out.println();
        }

    }

.........................................................................

OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern5() {
        for(int row=1;row<=9;row++) {
            for(int col=1;col<=9;col++) {
                if(row==1|| row==9|| row==5|| col==1&&row<=5|| col==9&&row>=5) {
                    System.out.print("* ");
                }
                else {
                    System.out.print("  ");
                    }
            }
            System.out.println();
        }

    }

.........................................................................

OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern6() {
        for(int row=1;row<=9;row++) {
            for(int col=1;col<=9;col++) {
                if(row==1|| row==9|| col==1|| row==5 || col==9&&row>=5) {
                    System.out.print("* ");
                }
                else {
                    System.out.print("  ");
                    }
            }
            System.out.println();
        }

    }


    }

.........................................................................

OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern7() {

            for(int row=1;row<=9;row++) {
                for(int col=1;col<=9;col++) {
                    if(col==8|| row==1&&col<=8) {
                        System.out.print("* ");
                    }
                    else {
                        System.out.print("  ");
                        }
                }
                System.out.println();
            }

        }
.........................................................................
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern8() {

            for(int row=1;row<=9;row++) {
                for(int col=1;col<=9;col++) {
                    if(row==1|| row==9|| col==1|| row==5 || col==9) {
                        System.out.print("* ");
                    }
                    else {
                        System.out.print("  ");
                        }
                }
                System.out.println();
            }




    }
.........................................................................

OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern9() {

            for(int row=1;row<=9;row++) {
                for(int col=1;col<=9;col++) {
                    if(row==1|| col==9|| row==5|| col==1&&row<=5) {
                        System.out.print("* ");
                    }
                    else {
                        System.out.print("  ");
                        }
                }
                System.out.println();
            }

        }


.........................................................................
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode
private static void pattern10() {

            for(int row=1;row<=9;row++) {
                for(int col=1;col<=9;col++) {
                    if(col==1||col==4|| col==9|| row==1&&col>=4|| row==9&&col>=4) {
                        System.out.print("* ");
                    }
                    else {
                        System.out.print("  ");
                        }
                }
                System.out.println();
            }

        }   
.........................................................................
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay