DEV Community

Neelakandan R
Neelakandan R

Posted on

1 1 1 1 1

forloop-pattern for num 1 2 3 4 5 6 7 8 9 & star pattern

program:1

package afterfeb13;

public class patternfornumber {
    public static void main(String[] args) {
        // number1();
        // number2();
        // number3();
        // number4();
        // number5();
        // number6();
        // number7();
        // number8();
        number9();

    }

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

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

            }
            System.out.println();
        }

    }

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

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

            }
            System.out.println();
        }

    }

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

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

            }
            System.out.println();
        }

    }

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

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

            }
            System.out.println();
        }

    }

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

            }
            System.out.println();
        }

    }

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

            }
            System.out.println();
        }

    }

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

            }
            System.out.println();
        }

    }

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

            }
            System.out.println();
        }

    }

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

            }
            System.out.println();
        }

    }

}

Enter fullscreen mode Exit fullscreen mode

program:2

package afterfeb13;

//54321 star
public class forpatternstar {
    public static void main(String[] args) {

        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++) {
                System.out.print("* ");

            }
            System.out.println();

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

program:3

package afterfeb13;

public class forpattern12345 {
    public static void main(String[] args) {

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

            }
            System.out.println();

        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

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

👋 Kindness is contagious

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

Okay