Exploring Java Patterns: Creative Approaches to Problem-Solving

Exploring Java Patterns: Creative Approaches to Problem-Solving

Patterns are a fascinating way to explore programming logic, understand loops, and sharpen problem-solving skills. Below, I share a series of pattern programs implemented in Java. These programs illustrate how creative use of loops can generate visually striking outputs, helping beginners learn while having fun with code.


1. Solid Rectangle Pattern

Pattern:


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

Code:

javaCopy codeimport java.util.Scanner;

class pattern1 {
    public static void main(String[] args) {
        int n, m;

        System.out.println("Enter Value of Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        System.out.println("Enter value of Columns");
        m = sc.nextInt();

        System.out.println("Output Pattern is:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

To Practice Follow ?

2. Hollow Rectangle Pattern

Pattern:


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

Code:

javaCopy codeimport java.util.Scanner;

class pattern2 {
    public static void main(String[] args) {
        int n, m;

        System.out.println("Enter Value of Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        System.out.println("Enter value of Columns");
        m = sc.nextInt();

        System.out.println("Output Pattern is:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.print("\n");
        }
    }
}

3. Right-Angled Triangle

Pattern:


*
**
***
****

Code:

javaCopy codeimport java.util.Scanner;

class pattern3 {
    public static void main(String args[]) {
        int n;

        System.out.println("Enter Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

4. Inverted Right-Angled Triangle

Pattern:


****
***
**
*

Code:

javaCopy codeimport java.util.Scanner;

class pattern4 {
    public static void main(String args[]) {
        int n;

        System.out.println("Enter Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

5. Pyramid Pattern

Pattern:

markdownCopy code   *
  **
 ***
****

Code:

javaCopy codeimport java.util.Scanner;

class pattern5 {
    public static void main(String args[]) {
        int n;

        System.out.println("Enter Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

6. Incremental Numbers Pattern

Pattern:


12
123
1234

Code:

javaCopy codeimport java.util.Scanner;

class pattern6 {
    public static void main(String args[]) {
        int n;

        System.out.println("Enter the number of Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.print("\n");
        }
    }
}

7. Decremental Numbers Pattern

Pattern:


123
12
1

Code:

javaCopy codeimport java.util.Scanner;

class pattern7 {
    public static void main(String args[]) {
        System.out.println("Enter the number of Rows");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i + 1; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

8. Floyd's Triangle Pattern

Pattern:


2 3
4 5 6
7 8 9 10

Code:

javaCopy codeimport java.util.Scanner;

class pattern8 {
    public static void main(String args[]) {
        int count = 1;
        int n;

        System.out.println("Enter Row Number");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(count + " ");
                count++;
            }
            System.out.println();
        }
    }
}

9. Binary Triangle

Pattern:


0 1
1 0 1
0 1 0 1

Logic: Alternating 1 and 0 based on the sum of indices being even or odd.

Code:

javaCopy codeimport java.util.Scanner;

class pattern9 {
    public static void main(String args[]) {
        int n;

        System.out.println("Enter Rows");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                if ((i + j) % 2 == 0) {
                    System.out.print("1 ");
                } else {
                    System.out.print("0 ");
                }
            }
            System.out.println();
        }
    }
}

Conclusion

Patterns are not just aesthetically pleasing but also foundational to learning loops and conditionals in programming. These programs are versatile for developers, providing a deeper understanding of nesting loops and controlling output formats. Try modifying these patterns to add your creativity, and watch your problem-solving skills grow!