DEV Community

Kaviya.k
Kaviya.k

Posted on

1

For loop Number pattern:

Numbers pattern(1to9):

package B1;

public class Npattern {

    public static void main(String[] args) {
        // Npattern1();
        // Npattern2();
        // Npattern3();
        // Npatern4();
        // Npattern5();
        // Npattern6();
        // Npattern7();
        // Npattern8();
        // Npattern9();
        Npattern10();

    }

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

    }
output:

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


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

    }
output:

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



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

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


    private static void Npattern7() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 1 || row + col == 10)
                    System.out.print("* ");

                else
                    System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

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

    private static void Npattern6() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    System.out.print("* ");
                else if (col == 1 || col == 9 && row >= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
output:

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


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

    }
output:

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


    private static void Npatern4() {

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

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

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

    }
output:

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


    private static void Npattern2() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 9 || row == 5 || row == 1)
                    System.out.print("* ");
                else if (col == 1 && row >= 5 || col == 9 && row <= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
output:

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


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

output:

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


Enter fullscreen mode Exit fullscreen mode

Star and numbers pattern:


package B1;

public class NSpattern {

    public static void main(String[] args) {
        pattern();
        // pattern1();
        // pattern2();
        // pattern3();
        // pattern4();
        // pattern5();
        // pattern6();

    }

    private static void pattern() {
        for (int row = 1; row <= 5; row++) {
            for (int col = 1; col <= row; col++)
                System.out.print("* ");

            System.out.println();
        }

    }
output:
* 
* * 
* * * 
* * * * 
* * * * * 


    private static void pattern6() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row + col + " ");
            System.out.println();
        }

    }
output:

6 7 8 9 10 
5 6 7 8 
4 5 6 
3 4 
2 


    private static void pattern5() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row / col + " ");
            System.out.println();
        }
    }
output:

5 2 1 1 1 
4 2 1 1 
3 1 1 
2 1 
1 


    private static void pattern4() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row * col + " ");
            System.out.println();
        }
    }
output:

5 10 15 20 25 
4 8 12 16 
3 6 9 
2 4 
1



    private static void pattern3() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(col + " ");
            System.out.println();
        }
    }
output:

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 



    private static void pattern2() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row + " ");
            System.out.println();
        }
    }
output:

5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

    private static void pattern1() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print("* ");
            System.out.println();
        }

    }

}
output:

* * * * * 
* * * * 
* * * 
* * 
* 
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs