34 Patterns Programs | JAVA Solutions| Patterns assignment Solutions

Build coding foundations for beginners.

34 Patterns Programs | JAVA Solutions| Patterns assignment Solutions

Photo by Ilya Pavlov on Unsplash

Table of contents


Printing patterns in your DSA journey is essential to build your firm grasp on the language and the loops. Given below are 34 pattern questions(solutions included). I strongly recommend you solve them yourself and only look at the solution if you get stuck in any problem.

Note: These questions were taken from Kunal kushwaha's assignment and here's the original link-
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/assignments/09-patterns.md

Questions :

1.  *****
    *****
    *****
    *****
    *****


2.  *
    **
    ***
    ****
    *****


3.  *****
    ****
    ***
    **
    *


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


5.  *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *


6.       *
        **
       ***
      ****
     *****


7.   *****
      ****
       ***
        **
         *


8.      *
       ***
      *****
     *******
    *********


9.  *********
     *******
      *****
       ***
        *


10.      *
        * *
       * * *
      * * * *
     * * * * *


11.  * * * * *
      * * * *
       * * *
        * *
         *


12.  * * * * *
      * * * *
       * * *
        * *
         *
         *
        * *
       * * *
      * * * *
     * * * * *


13.      *
        * *
       *   *
      *     *
     *********


14.  *********
      *     *
       *   *
        * *
         *


15.      *
        * *
       *   *
      *     *
     *       *
      *     *
       *   *
        * *
         *


16.           1
            1   1
          1   2   1
        1   3   3   1
      1   4   6   4   1


17.      1
        212
       32123
      4321234
       32123
        212
         1


18.   **********
      ****  ****
      ***    ***
      **      **
      *        *
      *        *
      **      **
      ***    ***
      ****  ****
      **********


19.    *        *
       **      **
       ***    ***
       ****  ****
       **********
       ****  ****
       ***    ***
       **      **
       *        *


20.    ****
       *  *
       *  *
       *  *
       ****

21.    1
       2  3
       4  5  6
       7  8  9  10
       11 12 13 14 15

22.    1
       0 1
       1 0 1
       0 1 0 1
       1 0 1 0 1

23.        *      *
         *   *  *   *
       *      *      *

24.    *        *
       **      **
       * *    * *
       *  *  *  *
       *   **   *
       *   **   *
       *  *  *  *
       * *    * *
       **      **
       *        *

25.       *****
         *   *
        *   *
       *   *
      *****

26.   1 1 1 1 1 1
      2 2 2 2 2
      3 3 3 3
      4 4 4
      5 5
      6

27.      *
        * *
       * * *
      * * * *
     * * * * *
      * * * *
       * * *
        * *
         *

28.      
       *        *
       **      **
       ***    ***
       ****  ****
       **********
       ****  ****
       ***    ***
       **      **
       *        *

29.         1
          2 1 2
        3 2 1 2 3
      4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5


30.      4 4 4 4 4 4 4  
         4 3 3 3 3 3 4   
         4 3 2 2 2 3 4   
         4 3 2 1 2 3 4   
         4 3 2 2 2 3 4   
         4 3 3 3 3 3 4   
         4 4 4 4 4 4 4   

31.    E
       D E
       C D E
       B C D E
       A B C D E

32.    a
       B c
       D e F
       g H i J
       k L m N o

33.    E D C B A
       D C B A
       C B A
       B A
       A

34.    1      1
       12    21
       123  321
       12344321

Solutions:

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

    }
    static void pattern1(int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern2(int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern3(int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern4(int n){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
    static void pattern5(int n){
        for (int i = 1; i < 2*n; i++) {
            int c = (i <= n)? i : 2*n-i;
            for (int j = 1; j <= c; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern6(int n){
        for (int i = 0; i < n; i++) {
            for (int s = 0; s < n - i - 1; s++) {
                System.out.print(" ");
            }
            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern7(int n){
        for (int i = 0; i < n; i++) {
            for (int s = 0; s < i+1; s++) {
                System.out.print(" ");
            }
            for (int j = 0; j < n - i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern8(int n){
        for (int i = 0; i < n; i++) {
            for (int s = 0; s < n-i; s++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2*i+1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern9(int n){
        for (int i = 0; i < n; i++) {
            for (int s = 0; s < i; s++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * n - 1-2*i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    static void pattern10(int n){
        for (int i = 0; i < n; i++) {
            for (int s = 0; s < n-i-1; s++) {
                System.out.print(" ");
            }
            for (int j = 0; j < i + 1; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
    static void pattern11(int n){
        for (int i = 0; i < n; i++) {
            for (int s = 0; s < i; s++) {
                System.out.print(" ");
            }
            for (int j = 0; j < n - i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
    static void pattern12(int n){
        for (int i = 0; i < 2 * n; i++) {
            int spaces = (i < n)? i:2*n-i-1;
            for (int s = 0; s < spaces; s++) {
                System.out.print(" ");
            }
            int c = (i < n)? n-i: i-n+1;
            for (int j = 0; j < c; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
    static void pattern13(int n){
        for (int i = 0; i < n; i++) {
            for (int out_s = 0; out_s < n-i-1; out_s++) {
                System.out.print(" ");
            }
            int c = (i < 2)? i+1: (i == n-1)? 2*n-1:2;
            for (int j = 0; j < c; j++) {
                System.out.print("*");
                for (int in_s = 0; i != n-1 && in_s < 2*i-1; in_s++) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern14(int n){
        for (int i = 0; i < n; i++) {
            for (int out_s = 0; out_s < i; out_s++) {
                System.out.print(" ");
            }
            int c = (i == 0)? 2*n-1: (i < 4)? 2:1;
            for (int j = 0; j < c; j++) {
                System.out.print("*");
                for (int in_s = 0; i != 0 && in_s < n-2*(i-1); in_s++) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern15(int n){
        for (int i = 0; i < 2 * n - 1; i++) {
            int out_spaces = (i < n)? n-i-1: i-n+1;
            for (int out_s = 0; out_s < out_spaces; out_s++) {
                System.out.print(" ");
            }
            int c = (i == 0 || i == 2*n-2)? 1:2;
            for (int j = 0; j < c; j++) {
                System.out.print("*");
                int in_spaces = (i < n)? 2*i-1: 3*n-2*(i-1)-2;
                for (int in_s = 0; in_s < in_spaces; in_s++) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern16(int n){
        for (int i = 1; i <= n; i++) {
            for (int s = 1; s <= 2*(n-i); s++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(findElement(i-1,j-1)+"   ");
            }
            System.out.println();
        }
    }
    static void findRowOfPascalTriangle(int R){
        int ans = 1;
        for (int i = 1; i <= R; i++) {
            ans *= (i == 1)? 1:(R-i+1);
            ans /= (i==1)? 1:(i-1);
            System.out.print(ans+" ");
        }
    }

    private static int findElement(int i, int j) {
        long ans = 1;
        for (int k = 0; k < j; k++) {
            ans *= i--;
        }
        long div = 1;
        int l = j;
        for (int k = 0; k < l; k++) {
            div *= j--;
        }
        long res = ans/div;
        return (int)res;
    }

    static void pattern17(int n){
        for (int i = 1; i <= 2*n-1; i++) {
            int spaces = (i <= n)? n-i: i-n;
            for (int s = 1; s <= spaces; s++) { //initial spaces
                System.out.print(" ");
            }
            int start = (i <= n)? i:2*n-i;
            for (int j = start; j >= 1; j--) {
                System.out.print(j);
            }
            for (int j = 2; j <= start; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
    static void pattern18(int n){
        for (int i = 1; i <= 2*n; i++) {
            int start = (i <= n)? n-i+2:i-n+1;
            int end = (i <= n)? n+i-1:3*n-i;
            for (int j = 1; j <= 2*n; j++) {
                if(j >= start && j <= end){
                    System.out.print(" ");
                } else { // between start and end print spaces
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
    static void pattern19(int n){
        for (int i = 1; i <= 2*n-1; i++) {

            for (int j = 1; j <= 2*n; j++) {
                if((i<= n && j <= i) || (i<= n && j > 2*n-i) || (i > n && j > i) || (i > n && j < 2*n+1-i)){
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern20(int n){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < n; j++) {
                if(i == 1 || i == n || j == 1 || j == n-1){
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern21(int n){
        int element = 1;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= i; j++) {
                System.out.print(element+++" ");
            }
            System.out.println();
        }
    }
    static void pattern22(int n){
        int element = 1;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= i; j++) {
                System.out.print(element);
                element = 1-element;
            }
            System.out.println();
            if(i%2 == 0){
                element = 1-element;
            }
        }
    }
    static void pattern23(int n){
        int pos1 = 3, pos2 = 2, pos3 = 1;
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 2*n-1; j++) {
                if(i == 1){
                    if(j == pos1) {
                        System.out.print("*");
                        pos1 += 4;
                    } else {
                        System.out.print(" ");
                    }
                } else if (i == 2) {
                    if(j == pos2) {
                        System.out.print("*");
                        pos2 += 2;
                    } else {
                        System.out.print(" ");
                    }
                } else {
                    if(j == pos3) {
                        System.out.print("*");
                        pos3 += 4;
                    } else {
                        System.out.print(" ");
                    }
                }
            }
            System.out.println();
        }
    }
    static void pattern24(int n){
        for (int i = 1; i <= 2*n; i++) {
            int printStar = (i <= n)? i: 2*n-i+1;
            for (int j = 1; j <= 2*n; j++) {
                if(j == 1 || j == 2*n || (j == printStar || j == 2*n-printStar+1)){
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern25(int n){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= n; j++) {
                if(i == 1 || i == n || (j == 1 || j == n)){
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern26(int n){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-i+1; j++) {
                System.out.print(i+" ");
            }
            System.out.println();
        }
    }
    static void pattern27(int n){
        for (int i = 1; i <= 2*n-1; i++) {
            int spaces = (i <= n)? n-i:i-n;
            for (int s = 1; s <= spaces; s++) {
                System.out.print(" ");
            }
            int c = (i <= n)? i:2*n-i;
            for (int j = 1;j <= c;j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
    static void pattern28(int n){
        for (int i = 1; i <= 2*n-1; i++) {
            int printStar = (i<=n)? i:2*n-i;
            for (int j = 1; j <= 2*n; j++) {
                if(j == 1 || j == 2*n || (j <= printStar || j > 2*n - printStar)){
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void pattern29(int n){
        for (int i = 1; i <= n; i++) {
            for (int s = 1; s <= 2*(n-i); s++) {
                System.out.print(" ");
            }
            for (int j = i; j >= 1; j--) {
                System.out.print(j+" ");
            }
            for (int j = 2; j <= i; j++) {
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
    static void pattern30(int n){
        for (int i = 1; i <= 2*n-1; i++) {
            for (int j = 1; j <= 2*n-1; j++) {
                int element = n-Math.min(Math.min(j-1,2*n-1-j),Math.min(i-1,2*n-1-i));
                System.out.print(element);
            }
            System.out.println();
        }
    }

    static void pattern31(int n){
        for (int i = 1; i <= n; i++) {
            for (int j = i; j >= 1; j--) {
                System.out.print((char)('A'+n-j)+" ");
            }
            System.out.println();
        }
    }
    static void pattern32(int n){
        char c = 'a';
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(c);
                c++;
            }
            System.out.println();
        }
    }
    static void pattern33(int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n-i; j++) {
                System.out.print((char)('A'+n-j-i-1)+" ");
            }
            System.out.println();
        }
    }
    static void pattern34(int n){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            for (int s = 1; s <= 2*(n-i); s++) {
                System.out.print(" ");
            }
            for (int j = i; j >= 1; j--) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}