Pattern Programs in Java Set – 1

9394
Pattern Program in Java Set-1
Advertisement

Pattern Programs are combination of different types of patterns mixed with different characters or special characters. Pattern Program is frequently asked in Interviews for Freshers because the Organization/Company want to check Candidate’s Logical Skills as well as Coding Skills.We are collecting the some similar shape of pattern with different types in the Pattern Programs in Java (Set-1). I hope this post will help you.

There are other different types of pattern programs you should also learn:

Pattern Program in Java

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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                System.out.print("*");

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

6

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

Pattern 2 :

11111
22222
33333
44444
555555

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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                System.out.print(i);

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

5

11111
22222
33333
44444
555555

Pattern 3:

11111
12111
11311
11141
11115

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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                if (i == j) {
                    System.out.print(i);
                } else {
                    System.out.print("1");
                }

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

6

111111
121111
113111
111411
111151
111116

Program 4:

11111
12111
12311
12341
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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                System.out.print(j);

            }

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

                System.out.print("1");

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

7

1111111
1211111
1231111
1234111
1234511
1234561
1234567

Pattern 5:

11111
22111
33311
44441
555555

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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                System.out.print(i);

            }

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

                System.out.print("1");

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

6

111111
221111
333111
444411
555551
666666

Program 6:

12345
12345
12345
12345
12345

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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                System.out.print(j);

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

6

123456
123456
123456
123456
123456
123456

Pattern 7:

55555
44444
33333
22222
11111

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 numbers of rows : ");

        int n = sc.nextInt();

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

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

                System.out.print(i);

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

6

666666
555555
444444
333333
222222
111111

Pattern 8:

54321
54321
54321
54321
54321

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 numbers of rows : ");

        int n = sc.nextInt();

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

            for (int j = n; j >= 1; j--) {

                System.out.print(j);

            }

            System.out.println();
        }
    }
}

Output:

Enter the numbers of rows :

6

654321
654321
654321
654321
654321
654321

Pattern 9:

ABCDE
ABCDE
ABCDE
ABCDE
ABCDE

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 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 = 'A'; i <= c; i++) {

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

                System.out.print(j);

            }

            System.out.println();
        }
    }
}

Output:

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

F

ABCDEF
ABCDEF
ABCDEF
ABCDEF
ABCDEF
ABCDEF

Program 10:

AAAAA
BBBBB
CCCCC
DDDDD
EEEEE

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 = 'A'; i <= c; i++) {

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

                System.out.print(i);

            }

            System.out.println();
        }
    }
}

Output:

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

F

AAAAAA
BBBBBB
CCCCCC
DDDDDD
EEEEEE
FFFFFF

Pattern 11:

EEEEE
DDDDD
CCCCC
BBBBB
AAAAA

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 (char j = 'A'; j <= c; j++) {

                System.out.print(i);

            }

            System.out.println();
        }
    }
}

Output:

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

F

FFFFFF
EEEEEE
DDDDDD
CCCCCC
BBBBBB
AAAAAA

Pattern 12:

EDCBA
EDCBA
EDCBA
EDCBA
EDCBA

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 = 'A'; i <= c; i++) {

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

                System.out.print(j);

            }

            System.out.println();
        }
    }
}

Output:

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

F

FEDCBA
FEDCBA
FEDCBA
FEDCBA
FEDCBA
FEDCBA

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

There are Data Structure Tutorials that you should learn:

You may also learn these to build the Knowledge: