Java Patterns - Level 2

20+ Java Pattern Questions With Solutions

Java Patterns - Level 2

Pattern Programs in Java Based on:

  • Numeric Patterns

  • Character Patterns

  • Star Patterns in Java

  • Mixed patterns

Pattern Programs - level 2

Pattern Program 27::

    A
   BB
  CCC
 DDDD
EEEEE
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        int i,j;
        for ( i=65;i<=69;i++){
            for ( j=69;j>i;j--)
                System.out.print(" ");
            for (j=65;j<=i;j++)
                System.out.print((char)i);
            System.out.println();
        }
    }
}

Pattern Program 28::

    A
   BA
  CBA
 DCBA
EDCBA
public class Patterns {
    public static void main(String[] args) {
        char i,j;
        for ( i='A';i<='E';i++){
            for ( j=i;j<'E';j++)
                System.out.print(" ");
            for (j=i;j>='A';j--)
                System.out.print(j);
            System.out.println();
        }
    }
}

Pattern Program 29::

    A
   AB
  ABC
 ABCD
ABCDE
public class Patterns {
    public static void main(String[] args) {
        char i,j;
        for ( i='A';i<='E';i++){
            for ( j=i;j<'E';j++)
                System.out.print(" ");
            for (j='A';j<=i;j++)
                System.out.print(j);
            System.out.println();
        }
    }
}

Pattern Program 30::

    E
   DD
  CCC
 BBBB
AAAAA
public class Patterns {
    public static void main(String[] args) {
        char i,j;
        for ( i='E';i>='A';i--){
            for ( j=i;j>'A';j--)
                System.out.print(" ");
            for (j='E';j>=i;j--)
                System.out.print(i);
            System.out.println();
        }
    }
}

Pattern Program 31::

65 65 65 65
66 66 66 66
67 67 67 67
68 68 68 68
public class Patterns {
    public static void main(String[] args) {
        int i,j;
        for ( i=65;i<=68;i++){
            for ( j=65;j<=68;j++)
                System.out.print(i+" ");

            System.out.println();
        }
    }
}

Pattern Program 32::

    5 
   54
  543
 5432
54321
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=5;i>=1;i--){
            for ( j=i;j>1;j--)
                System.out.print(" ");
            for (j=5;j>=i;j--)
                System.out.print(j);
            System.out.println();
        }
    }
}

Pattern Program 33::

    A
   BA
  CBA
 DCBA
EDCBA
public class Patterns {
    public static void main(String[] args) {
        char i,j;
        for ( i='A';i<='E';i++){
            for ( j=i;j<'E';j++)
                System.out.print(" ");
            for (j=i;j>='A';j--)
                System.out.print(j);
            System.out.println();
        }
    }
}

Pattern Program 34::

Enter the number: 5
    1
   22
  333
 4444
55555
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.print("Enter the number: ");
         int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for ( j=i;j<n;j++)
                System.out.print(" ");
            for (j=i;j>=1;j--)
                System.out.print(i);
            System.out.println();
        }
    }
}

Pattern Program 35::

    5
   44
  333
 2222
11111
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=5;i>=1;i--){
            for ( j=i;j>1;j--)
                System.out.print(" ");
            for (j=i;j<=5;j++)
                System.out.print(i);
            System.out.println();
        }
    }
}

Pattern Program 36::

    5
   45
  345
 2345
12345
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=5;i>=1;i--){
            for ( j=i;j>1;j--)
                System.out.print(" ");
            for (j=i;j<=5;j++)
                System.out.print(j);
            System.out.println();
        }
    }
}

Pattern Program 37::

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

Pattern Program 38::

Enter the value of n: 5
    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for ( j=1;j<=n-i;j++)
                System.out.print(" ");
            for (j=1;j<=i;j++)
                System.out.print(j+" ");
            System.out.println();
        }
        for (i=n-1;i>=1;i--){
            for ( j=1;j<=n-i;j++)
                System.out.print(" ");
            for (j=1;j<=i;j++)
                System.out.print(j+" ");
            System.out.println();
        }
    }
}

Pattern Program 39::

Enter the value of n: 5
    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5
 4 4 4 4
  3 3 3
   2 2
    1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for ( j=1;j<=n-i;j++)
                System.out.print(" ");
            for (j=1;j<=i;j++)
                System.out.print(i+" ");
            System.out.println();
        }
        for (i=n-1;i>=1;i--){
            for ( j=1;j<=n-i;j++)
                System.out.print(" ");
            for (j=1;j<=i;j++)
                System.out.print(i+" ");
            System.out.println();
        }
    }
}

Pattern Program 40::

Enter the value of n: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j,k=1;
        for ( i=1;i<=n;i++){
            for (j=1;j<=i;j++)
                System.out.print(k++ +" ");
            System.out.println();
        }
    }
}

Pattern Program 41::

Enter the value of n: 5
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=1;j<=i;j++) {
                if (j % 2 == 0) {
                    System.out.print("0" + " ");
                } else {
                    System.out.print("1" + " ");
                }
            }
            System.out.println();
        }
    }
}

Pattern Program 42::

Enter the value of n: 5
1
0 0
1 1 1
0 0 0 0
1 1 1 1 1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=1;j<=i;j++) {
                if (i % 2 == 0) {
                    System.out.print("0" + " ");
                } else {
                    System.out.print("1" + " ");
                }
            }
            System.out.println();
        }
    }
}

Pattern Program 43::

Enter the value of n: 5
1 1 1 1 1
0 0 0 0
1 1 1
0 0
1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=n;j>=i;j--) {
                if (i % 2 == 0) {
                    System.out.print("0" + " ");
                } else {
                    System.out.print("1" + " ");
                }
            }
            System.out.println();
        }
    }
}

Pattern Program 44::

Enter the value of n: 5
     1
    1 0
   1 0 1
  1 0 1 0
 1 0 1 0 1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=i;j<=n;j++) {
                System.out.print(" ");
            }
            for (j=1;j<=i;j++){
                if (j % 2 == 0) {
                    System.out.print("0" + " ");
                } else {
                    System.out.print("1" + " ");
                }
            }
            System.out.println();
        }
    }
}

Pattern Program 45::

Enter the value of n: 5
     1
    0 0
   1 1 1
  0 0 0 0
 1 1 1 1 1
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=i;j<=n;j++) {
                System.out.print(" ");
            }
            for (j=1;j<=i;j++){
                if (i % 2 == 0) {
                    System.out.print("0" + " ");
                } else {
                    System.out.print("1" + " ");
                }
            }
            System.out.println();
        }
    }
}

Pattern Program 46::

Enter the value of n: 5
* * * * *
# # # # #
* * * * *
# # # # #
* * * * *
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=1;j<=n;j++) {
                if (i % 2 == 0) {
                    System.out.print("#" + " ");
                } else {
                    System.out.print("*" + " ");
                }
            }
            System.out.println();
        }
    }
}

Pattern Program 47::

Enter the value of n: 5
# # # # #
* * * * *
# # # # #
* * * * *
# # # # #
import java.util.Scanner;
public class Patterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n=sc.nextInt();
        int i,j;
        for ( i=1;i<=n;i++){
            for (j=1;j<=n;j++) {
                if (i % 2 == 0) {
                    System.out.print("*" + " ");
                } else {
                    System.out.print("#" + " ");
                }
            }
            System.out.println();
        }
    }
}