Pattern Programs in Java Set – 5

6690
Pattern Program set 5
Advertisement

Pattern Programs in Java Set – 5

Pattern Program in Java 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 = 1; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= n; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
*****
 ****
  ***
   **
    *

Pattern 2:

11111
 2222
  333
   44
    5

Java Program:

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 = 1; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= n; k++) {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
11111
 2222
  333
   44
    5

Pattern 3:

12345
 1234
  123
   12
    1

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 = 1;j<i;j++){
                System.out.print(" ");
            }
            for(int k=1;k<=n-i+1;k++){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
12345
 1234
  123
   12
    1

Pattern 4:

12345
 2345
  345
   45
    5

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 = 1;j<i;j++){
                System.out.print(" ");
            }
            for(int k=i;k<=n;k++){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
6
123456
 23456
  3456
   456
    56
     6

Pattern 5:

55555
 4444
  333
   22
    1

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 number of rows : ");
        int n = sc.nextInt();
        for(int i = n;i>=1;i--){
            for(int j = n;j>i;j--){
                System.out.print(" ");
            }
            for(int k=i;k>=1;k--){
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
55555
 4444
  333
   22
    1

Pattern 6:

54321
 5432
  543
   54
    5

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 number of rows : ");
        int n = sc.nextInt();
        for(int i = n;i>=1;i--){
            for(int j = n;j>i;j--){
                System.out.print(" ");
            }
            for(int k=n;k>=n-i+1;k--){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
54321
 5432
  543
   54
    5

Pattern 7:

54321
 4321
  321
   21
    1

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 = n;i>=1;i--){
            for(int j = n;j>i;j--){
                System.out.print(" ");
            }
            for(int k=i;k>=1;k--){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
54321
 4321
  321
   21
    1

Pattern 8:

AAAAA
 BBBB
  CCC
   DD
    E

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 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 = 'A';j<i;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
AAAAA
 BBBB
  CCC
   DD
    E

Pattern 9:

ABCDE
 ABCD
  ABC
   AB
    A

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 case
        //Enter input carefully
        char c = sc.next().charAt(0);
        for(int i = 1;i<=c-'A'+1;i++){
            for(int j = 1;j<i;j++){
                System.out.print(" ");
            }
            for(char k='A';k<=c-i+1;k++){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
ABCDE
 ABCD
  ABC
   AB
    A

Pattern 10:

ABCDE
 BCDE
  CDE
   DE
    E

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 case
        //Enter input carefully
        char c = sc.next().charAt(0);
        for(char i = 'A';i<=c;i++){
            for(int j = 'A';j<i;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
ABCDE
 BCDE
  CDE
   DE
    E

Pattern 11:

EEEEE
 DDDD
  CCC
   BB
    A

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 case
        //Enter input carefully
        char c = sc.next().charAt(0);
        for(char i = c;i>='A';i--){
            for(char j = c;j>i;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
EEEEE
 DDDD
  CCC
   BB
    A

Pattern 12:

EDCBA
 EDCB
  EDC
   ED
    E

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 case
        //Enter input carefully
        char c = sc.next().charAt(0);
        for(int i = 'A';i<=c;i++){
            for(char j = 'A';j<i;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
EDCBA
 EDCB
  EDC
   ED
    E

Pattern 13:

EDCBA
 DCBA
  CBA
   BA
    A

Pattern Program in Java:

import java.util.Scanner;
class Pattern_13 {
    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 = c;i>='A';i--){
            for(char j = c;j>i;j--){
                System.out.print(" ");
            }
            for(char k=i;k>='A';k--){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
EDCBA
 DCBA
  CBA
   BA
    A

Pattern 14:

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

Pattern Program in Java:

import java.util.Scanner;
class Pattern_14 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows : ");
        //There is difference A-Z in Capitals and a-z in small case
        //Enter input carefully
        int n = sc.nextInt();
        int num = 1;
        for(int i = 1;i<=n;i++){
            for(int j = 1;j < i;j++){
                System.out.print(" ");
            }
            for(int k=i;k<=n;k++){
                System.out.print(num+" ");
                num++;
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
 1 2 3 4 5 
   6 7 8 9 
  10 11 12 
     13 14 
        15

Pattern 15:

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

Pattern Program in Java:

import java.util.Scanner;
public class Pattern_15 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows : ");
        int n = sc.nextInt();
        int num = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i + 1; j++) {
                System.out.print(num + " ");
                num++;
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows :
5
1  2  3  4  5
6  7  8  9
10 11 12
13 14
15

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

Learn Data Structure in java