Untitled

 avatar
unknown
plain_text
6 months ago
1.2 kB
7
Indexable
public class NumberPattern {

    public static void main(String[] args) {
        printPattern();
    }

    // Static method to print the desired pattern
    public static void printPattern() {
        int n = 9; // The maximum number in the pattern
        int i = 0; // Row counter

        while (i < n) {
            int j = 0; // Column counter for leading spaces
            // Print leading spaces to create the triangle effect
            while (j < i) {
                System.out.print(" "); // One space for alignment
                j++;
            }

            int num = i + 1; // Starting number for the current row
            // Print increasing part
            int k = 0;
            while (k < n - i) {
                System.out.print(num++);
                k++;
            }

            // Print decreasing part
            num -= 2; // Adjust to the last printed number before decreasing
            k = 0;
            while (k < n - i - 1) {
                System.out.print(num--);
                k++;
            }

            // Move to the next line
            System.out.println();
            i++;
        }
    }
}
Editor is loading...
Leave a Comment