Pattern Programs in Java Set – 4

1551
Pattern Set 4
Advertisement

Pattern Program in Java (Set – 4):

Pattern Programs can be Number Pattern, Alphabets or Special Symbols. Patterns are frequently asked in Interviews for Freshers because the Organization/Company want to check Candidate’s Logical Skills as well as Coding Skills. This post is the collection of some similar shape of pattern with different types. I hope this post will definitely help you.


Pattern 1:

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

Pattern Program in Java:

import java.util.Scanner;

class Pattern_1 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {

            for (int j = n - i; j > 0; j--) {

                System.out.print(" ");
            }

            for (int k = 1; k <= i; k++) {

                System.out.print("*");
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

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

Pattern 2:

    1
   11
  111
 1111
11111

Pattern Program in Java:

import java.util.Scanner;

class Pattern_2 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for(int i = 1;i <= n;i++){

            for(int j = n-i;j > 0;j--){

                System.out.print(" ");
            }

            for(int k = 1;k <= i;k++){

                System.out.print("1");
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

    1
   11
  111
 1111
11111

Pattern 3:

    1
   22
  333
 4444
55555

Pattern Program in Java:

import java.util.Scanner;

class Pattern_3 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for(int i = 1;i <= n;i++){

            for(int j = n-i;j > 0;j--){

                System.out.print(" ");
            }

            for(int k = 1;k <= i;k++){

                System.out.print(i);
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

    1
   22
  333
 4444
55555

Pattern 4:

    1
   12
  123
 1234
12345

Pattern Program in Java:

import java.util.Scanner;

class Pattern_4 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for(int i = 1;i <= n;i++){

            for(int j = n-i;j > 0;j--){

                System.out.print(" ");
            }

            for(int k = 1;k <= i;k++){

                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

    1
   12
  123
 1234
12345

Pattern 5:

    A
   BB
  CCC
 DDDD
EEEEE

Pattern Program in Java:

import java.util.Scanner;

class Pattern_5 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the word to limit the Pattern (In Capitals) : ");

        //There is difference A-Z in Capitals and a-z in small case
        //Enter input carefully

        char c = sc.next().charAt(0);

        for (char i = 'A'; i <=c; i++){

            for (int j = c - i; j > 0 ; j--){

                System.out.print(" ");
            }

            for (char k = 'A'; k <=i; k++){

                System.out.print(i);
            }

            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E

    A
   BB
  CCC
 DDDD
EEEEE

Pattern 6:

    A
   AB
  ABC
 ABCD
ABCDE

Pattern Program in Java:

import java.util.Scanner;

class Pattern_6 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the word to limit the Pattern (In Capitals) : ");

        //There is difference A-Z in Capitals and a-z in small case
        //Enter input carefully

        char c = sc.next().charAt(0);

        for (char i = 'A'; i <= c; i++) {

            for (int j = c - i; j > 0; j--) {

                System.out.print(" ");
            }

            for (char k = 'A'; k <= i; k++) {

                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E

    A
   AB
  ABC
 ABCD
ABCDE

Pattern 7:

    5
   54
  543
 5432
54321

Pattern Program in Java:

import java.util.Scanner;

class Pattern_7 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {

            for (int j = n - i; j > 0; j--) {

                System.out.print(" ");
            }

            for (int k = n; k > n - i; k--) {

                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

    5
   54
  543
 5432
54321

Pattern 8:

    5
   45
  345
 2345
12345

Pattern Program in Java:

import java.util.Scanner;

class Pattern_8 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {

            for (int j = n - i; j > 0; j--) {

                System.out.print(" ");
            }

            for (int k = n - i + 1; k <= n; k++) {

                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

    5
   45
  345
 2345
12345

Pattern 9:

    5
   44
  333
 2222
11111

Pattern Program in Java:

import java.util.Scanner;

class Pattern_9 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows : ");

        int n = sc.nextInt();

        for (int i = n; i > 0; i--) {

            for (int j = i - 1; j > 0; j--) {

                System.out.print(" ");
            }

            for (int k = 1; k <= n - i + 1; k++) {

                System.out.print(i);
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5

    5
   44
  333
 2222
11111

Pattern 10:

    E
   ED
  EDC
 EDCB
EDCBA

Pattern Program in Java:

import java.util.Scanner;

class Pattern_10 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the word to limit the Pattern (In Capitals) : ");

        // There is difference A-Z in Capitals and a-z in small cases
        //Enter input Carefully

        char c = sc.next().charAt(0);

        for(char i = c;i >= 'A';i--){

            for(int j = i-1;j >= 'A';j--){

                System.out.print(" ");
            }

            for(char k = c;k >= i;k--){

                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E

    E
   ED
  EDC
 EDCB
EDCBA

Pattern 11:

    E
   DE
  CDE
 BCDE
ABCDE

Pattern Program in Java:

import java.util.Scanner;

class Pattern_11 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the word to limit the Pattern (In Capitals) : ");

        // There is difference A-Z in Capitals and a-z in small cases
        //Enter input Carefully

        char c = sc.next().charAt(0);

        for (char i = c; i >= 'A'; i--) {

            for (int j = i - 1; j >= 'A'; j--) {

                System.out.print(" ");
            }

            for (char k = i; k <= c; k++) {

                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E

    E
   DE
  CDE
 BCDE
ABCDE

Pattern 12:

    E
   DD
  CCC
 BBBB
AAAAA

Pattern Program in Java:

import java.util.Scanner;

class Pattern_12 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the word to limit the Pattern (In Capitals) : ");

        // There is difference A-Z in Capitals and a-z in small cases
        //Enter input Carefully

        char c = sc.next().charAt(0);

        for(char i = c;i >= 'A';i--){

            for(int j = i-1;j >= 'A';j--){

                System.out.print(" ");
            }

            for(char k = i;k <= c;k++){

                System.out.print(i);
            }

            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E

    E
   DD
  CCC
 BBBB
AAAAA

I hope you will find these Pattern Programs very helpful for your preparation.If you have doubts regarding these programs comment below or directly mail us.We will happy to help you.

If you need more Pattern Programs for Practice.Please follow the Link : 

Pattern Programs in Java

Learn Data Structure in Java:

Data Structure in Java