Pattern Program in Java Set – 6

0
2661
Triangle Pattern
Triangle Pattern
Advertisement

Triangle Pattern Programs

Pattern Programs is the collection of Pyramid Patterns and Triangle Pattern Programs. These Pattern Programs frequently asked in the Online Programming Test or Interview to check your Logical and Coding Skills. There is the collection of Pyramid type pattern Programs that could help you in your Interviews. I hope This post could help you the most.

In this post we are printing the Triangle Pattern Programs with different symbols and character and numbers.

Pattern 1:

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

Pattern Program in Java:

import java.util.Scanner;

public 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 = n; i >= 1; i--) {

            for (int j = i - 1; j >= 0; j--) {
                System.out.print(" ");
            }

            for (int k = n; k >= i; k--) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

Output:

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

Pattern 2:

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

Pattern Program in Java:

import java.util.Scanner;

public 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 = (2 * i - 1); k >= 1; k--) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

Output:

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

Pattern 3:

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

Pattern Program in Java:

import java.util.Scanner;

public 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 = 2 * i - 1; k >= 1; k--) {
                System.out.print(i + " ");
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
        1 
      2 2 2 
    3 3 3 3 3 
  4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 5

Pattern 4:

        1 
      3 3 3 
    5 5 5 5 5 
  7 7 7 7 7 7 7 
9 9 9 9 9 9 9 9 9

Pattern Program in Java:

import java.util.Scanner;

public 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();

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

            for (int j = n - i; j > 0; j--) {
                System.out.print("  ");
            }

            for (int k = 2 * i - 1; k >= 1; k--) {
                System.out.print(num + " ");
            }

            num += 2;
            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
        1 
      3 3 3 
    5 5 5 5 5 
  7 7 7 7 7 7 7 
9 9 9 9 9 9 9 9 9

Pattern 5:

        A 
      B B B 
    C C C C C 
  D D D D D D D 
E E E E E E E E E

Pattern Program in Java:

import java.util.Scanner;

public 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 a small case
        //Enter input carefully

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

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

            for (int j = c; j > i; j--) {
                System.out.print("  ");
            }

            for (int k = 1; k <= (2 *var - 1); k++) {
                System.out.print(i + " ");
            }

            var++;
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
        A 
      B B B 
    C C C C C 
  D D D D D D D 
E E E E E E E E E

Pattern 6:

        A 
      C C C 
    E E E E E 
  G G G G G G G 
I I I I I I I I I 

Pattern Program in Java:

import java.util.Scanner;

public 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 a small case
        //Enter input carefully

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

        int var = 1;
        for (char i = 'A'; i <= c; i = (char)(i + 2)) {

            for (int j = c; j > i; j = j - 2) {
                System.out.print("  ");
            }

            for (int k = 1; k <= (2 *var - 1); k++) {
                System.out.print(i + " ");
            }

            var++;
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
I
        A 
      C C C 
    E E E E E 
  G G G G G G G 
I I I I I I I I I 

Pattern 7:

    1
   123
  12345
 1234567
123456789

Pattern Program in Java:

import java.util.Scanner;

public 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 = 1; k <= (2 * i - 1); k++) {
                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows : 
5
    1
   123
  12345
 1234567
123456789

Pattern 8:

    1
   321
  54321
 7654321
987654321

Pattern Program in Java:

import java.util.Scanner;

public 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 = (2 * i - 1); k > 0; k--) {
                System.out.print(k);
            }

            System.out.println();
        }

    }
}

Output:

Enter the number of rows : 
5
    1
   321
  54321
 7654321
987654321

Pattern 9:

        A 
      A B C 
    A B C D E 
  A B C D E F G 
A B C D E F G H I 

Pattern Program in Java:

import java.util.Scanner;

public 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 a small case
        // Enter input carefully
        char c = sc.next().charAt(0);

        int
                var = 1;

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

            char word = 'A';

            for (int j = c; j > i; j--) {
                System.out.print("  ");
            }

            for (int k = 1; k <= (2 *var - 1); k++) {
                System.out.print(word + " ");
                word++;
            }

            var++;
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
        A 
      A B C 
    A B C D E 
  A B C D E F G 
A B C D E F G H I 

Pattern 10:

        A 
      C B A 
    E D C B A 
  G F E D C B A 
I H G F E D C B A 

Pattern Program in Java:

import java.util.Scanner;

public class Pattern {

    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 a small case
        // Enter input carefully
        char c = sc.next().charAt(0);

        int var = 1;

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

            char word = 'A';

            for (int j = c; j > i; j--) {
                System.out.print("  ");
            }

            for (int k = (2*var - 1); k> 0; k--) {
                System.out.print((char)(word + k - 1) + " ");
            }

            var++;
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
        A 
      C B A 
    E D C B A 
  G F E D C B A 
I H G F E D C B A 

Pattern 11:

     0
    101
   21012
  3210123
 432101234

Pattern Program in Java:

import java.util.Scanner;

public class Pattern_11 {
    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 = 0; i < n; i++) {

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

            for (int k = i; k >= 0; k--){
                System.out.print(k);
            }

            for (int l = 1; l <= i; l++) {
                System.out.print(l);
            }
            System.out.println();

        }

    }
}

Output:

Enter the number of rows: 
5
     0
    101
   21012
  3210123
 432101234

Pattern 12:

    A
   BAB
  CBABC
 DCBABCD
EDCBABCDE

Pattern Program in Java:

import java.util.Scanner;

public 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 a small case
        // Enter input carefully

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

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

            for (int j = c; j > i; j--) {
                System.out.print(" ");
            }

            for (char k = i; k >= 'A'; k--) {
                System.out.print(k);
            }

            for (char l = 'B'; l <= i; l++) {
                System.out.print(l);
            }
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
    A
   BAB
  CBABC
 DCBABCD
EDCBABCDE

Pattern 13:

    1
   121
  12321
 1234321
123454321

Pattern Program in Java:

import java.util.Scanner;

public class Pattern_13 {
    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; j > i; j--) {
                System.out.print(" ");
            }

            for (int k = 1; k <= i; k++) {
                System.out.print(k);
            }
            
            for (int l = i - 1; l >= 1; l--) {
                System.out.print(l);
            }

            System.out.println();
        }

    }
}

Output:

Enter the number of rows : 
5
    1
   121
  12321
 1234321
123454321

Pattern 14:

    A
   ABA
  ABCAB
 ABCDABC
ABCDEABCD

Pattern Program in Java:

import java.util.Scanner;

public class Pattern {

    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 a small case
        // Enter input carefully

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

        for (char i = 'A'; i <= c; i++) {
            
            for (int j = c; j > i; j--) {
                System.out.print(" ");
            }

            for (char k = 'A'; k <= i; k++) {
                System.out.print(k);
            }
            
            for (char l = 'A'; l < i; l++) {
                System.out.print(l);
            }
            
            System.out.println();
        }
    }
}

Output:

Enter the word to limit the Pattern (In Capitals) : 
E
    A
   ABA
  ABCAB
 ABCDABC
ABCDEABCD

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 Triangle Pattern Programs for Practice. Please follow the Link: 

Pattern Programs

Want to learn about Data Structure in Java :

Data Structure in Java

LEAVE A REPLY

Please enter your comment!
Please enter your name here