Pattern Program in Java Set – 2

5652
Pattern Set 2
Advertisement

Pattern Programs in Java Set-2 can be Number Pattern, Alphabets, or Special Symbols. 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. This post is the collection of some similar shapes of patterns 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 numbers of rows : ");

    int n = sc.nextInt();

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

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

        System.out.print("*");

      }

      System.out.println();
    }
  }
}

Output:

Enter the numbers of rows :

6

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

Pattern 2:

1
12
123
1234
12345

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 <= i; j++) {

        System.out.print(j);

      }

      System.out.println();
    }
  }
}

Output:

Enter the numbers of rows :

6

1
12
123
1234
12345
123456

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

      }

      System.out.println();
    }
  }
}

Output:

Enter the numbers of rows :

6

1
22
333
4444
55555
666666

 

Pattern 4:

A
BB
CCC
DDDD
EEEEE

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 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 <= i; j++) {

        System.out.print(i);

      }

      System.out.println();
    }
  }
}

Output:

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

E

A
BB
CCC
DDDD
EEEEE

Pattern 5:

A
AB
ABC
ABCD
ABCDE

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 cases
    //Enter input Carefully
    char c = sc.next().charAt(0);

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

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

        System.out.print(j);

      }

      System.out.println();
    }
  }
}

Output:

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

F

A
AB
ABC
ABCD
ABCDE
ABCDEF

Pattern 6:

1
1 2
3 5 8
13 21 34 55
89 144 233 377 610

Pattern Program in Java:

import java.util.Scanner;

public class Fibonacci_Triangle {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System. in );

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

    int n = sc.nextInt();

    printFibonacci(n);

  }

  private static void printFibonacci(int n) {

    int N = n * (n + 1) / 2;
    int[] fibNumber = new int[N + 1];

    //Storing fibonacci Number in an array
    fibNumber[1] = 1;
    fibNumber[2] = 1;

    for (int i = 3; i <= N; i++) {
      fibNumber[i] = fibNumber[i - 1] + fibNumber[i - 2];
    }

    int fibIndex = 1;
    //Print the Fibonacci Number in the form of triangle
    for (int i = 1; i <= n; i++) {

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

        System.out.print(fibNumber[fibIndex++] + " ");
      }
      System.out.println();
    }
  }
}

Output:

Enter the number of rows :
5

1
1 2
3 5 8
13 21 34 55
89 144 233 377 610

 

Pattern 7:

0
01
011
0112
01123

Pattern Program in Java:

import java.util.Scanner;

public class Fibonacci_Pyramid {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System. in );

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

    int n = sc.nextInt();

    printFibonacci(n);

  }

  private static void printFibonacci(int n) {

    int N = n * (n + 1) / 2;
    int[] fibNumber = new int[N + 1];

    //Storing fibonacci Number in an array
    fibNumber[0] = 0;
    fibNumber[1] = 1;

    for (int i = 2; i <= N; i++) {
      fibNumber[i] = fibNumber[i - 1] + fibNumber[i - 2];
    }

    //Print the Fibonacci Number in the form of triangle
    for (int i = 1; i <= n; i++) {

      int fibIndex = 0;

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

        System.out.print(fibNumber[fibIndex++] + " ");
      }
      System.out.println();
    }
  }
}

Output:

Enter the number of rows :

5

0
0 1
0 1 1
0 1 1 2
0 1 1 2 3

Pattern 8:

1
2 3
4 5 6
7 8 9 10

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

    int num = 1;

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

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

        System.out.print(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 9:

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

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

    int n = sc.nextInt();

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

      int num = i;

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

        System.out.print(num + " ");
        num = num + (n - j);

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

Output:

Enter the number of rows :
5

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

Pattern 10:

1
2 4
1 3 5
2 4 6 8
1 3 5 7 9

Pattern Program in Java:

import java.util.Scanner;

public class Pattern_10 {

  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++) {

      int odd = 1;
      int even = 2;

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

        if (i % 2 == 1) {
          System.out.print(odd + " ");
          odd = odd + 2;
        } else {
          System.out.print(even + " ");
          even = even + 2;
        }

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

Output:

Enter the number of rows :
5


1
2 4
1 3 5
2 4 6 8
1 3 5 7 9

 

Pattern 11:

1
3 2
6 5 4
10 9 8 7

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

    int k = 0;

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

        System.out.print(j + " ");

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

Output:

Enter the number of rows :
5

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

Pattern 12:

     1 
   3 8 5 
7 9 40 11 33

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

    int n = sc.nextInt();

    int x = 1;
    int y = 1;
    int z = 1;

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

      for (int j = 0; j < y; j++) {

        if (i == j) {

          z = (x + 1) * (j + j);
          z = (z == 0) ? 1 : z;
          System.out.print(" " + z);
          continue;
        }
        x = x + 2;
        System.out.print(" " + x);
      }
      y = y + 2;
      System.out.println();
    }
  }
}

Output:

Enter the number of rows :
4

          1 
        3 8 5 
     7 9 40 11 13 
15 17 19 120 21 23 25

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

Find the Data Structure related resources:

You may also learn these to build the Knowledge: