July 3, 2021

Java Programs For Displaying Patterns

In this post we’ll have Java programs for displaying patterns which are the beginner level programs to understand and use loops in Java. In these Java programs outer and inner for loops are used to display patterns using numbers or symbols.

Java program for pyramid pattern – Pattern 1

A very popular pyramid patters is to display pyramid of numbers having the digit repeated as many times as the number in each row.

      1 
     2 2 
    3 3 3 
   4 4 4 4 
  5 5 5 5 5 
 6 6 6 6 6 6 
7 7 7 7 7 7 7 
public class PatternProgram {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=1; i<=rows; i++){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print number 
      for(int k=0; k<i; k++){
        System.out.print(i + " ");
      }
      System.out.println();           
    }     
  }
}

Java program for inverted pyramid – Pattern 2

If you want to display pyramid up side down.

7 7 7 7 7 7 7 
 6 6 6 6 6 6 
  5 5 5 5 5 
   4 4 4 4 
    3 3 3 
     2 2 
      1 
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=rows; i>=1; i--){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print number 
      for(int k=0; k<i; k++){
        System.out.print(i + " ");
      }
      System.out.println();           
    }     
  }
}

Java program for half pyramid – Pattern 3

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=1; i<=rows; i++){	            
      for(int j=0; j<i; j++){
        System.out.print(i + " ");
      }
      System.out.println();
              
    }    
  }
}

Java program for half pyramid – Pattern 4

              1 
            1 2 
          1 2 3 
        1 2 3 4 
      1 2 3 4 5 
    1 2 3 4 5 6 
  1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=1; i<=rows; i++){	
      // print correct number of spaces 
      // in each row
      for(int j=0; j<2*(rows-i); j++){
        System.out.print(" ");
      }
      for(int j=1; j<=i; j++){
        System.out.print(j+" ");
      }
      System.out.println();	
    } 
  }
}

Java program for half pyramid – Pattern 5

In this pattern numbers are in series rather than being reset in each row. This pattern is known as Floyd's triangle.

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    int num = 1;
    for(int i=1; i<=rows; i++){	            
      for(int j=0; j<i; j++){
        System.out.print(num++ + " ");
      }
      System.out.println();				
    } 
  }
}

Java program for pyramid pattern – Pattern 6

Pyramid where number increments and decrements in the same row.

       1
      121
     12321
    1234321
   123454321
  12345654321
 1234567654321
123456787654321
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=1; i<=rows; i++){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print incrementing part
      for(int k=1; k<i; k++){
        System.out.print(k);
      }
      // print decrementing part
      for(int k=i; k>=1; k--){
        System.out.print(l);
      }
      System.out.println();     
    }    
  }
}

Java program for pyramid pattern – Pattern 7

Pyramid using ‘*’ symbol.

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=1; i<=rows; i++){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print number 
      for(int k=0; k<i; k++){
        System.out.print("* ");
      }
      System.out.println();           
    }     
  }
}

Java program for pattern – Pattern 8

8 7 6 5 4 3 2 1 
8 7 6 5 4 3 2 
8 7 6 5 4 3 
8 7 6 5 4 
8 7 6 5 
8 7 6 
8 7 
8 
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=1; i<=rows; i++){	            
      for(int j=rows; j>=i; j--){
        System.out.print(j + " ");
      }
      System.out.println();            
    }    
  }
}

Java pattern program – Pattern 9

1
12
123
1234
12345
123456
1234567
12345678
1234567
123456
12345
1234
123
12
1
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    //For upper half-incrementing
    for(int i=1; i<=rows; i++){
      for(int j=1; j<=i; j++){
        System.out.print(j);
      }            
      System.out.println();            
    }
    //For lower half-decrementing
    for(int i=rows; i>=1; i--){
      for(int j=1; j<i; j++){
        System.out.print(j);
      }
      System.out.println();
    }     
  }
}

Java pattern program – Pattern 10

12345678
1234567
123456
12345
1234
123
12
1
12
123
1234
12345
123456
1234567
12345678
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=rows; i>=1; i--){
      for(int j=1; j<=i; j++){
        System.out.print(j);
      }	           
      System.out.println();	           
    }
    for(int i=2; i<=rows; i++){
      for(int j=1; j<=i; j++){
        System.out.print(j);
      }
      System.out.println();
    }     
  }
}

Java pattern program – Pattern 11

7777777
 666666
  55555
   4444
    333
     22
      1
     22
    333
   4444
  55555
 666666
7777777
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
    sc.close();
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=rows; i>=1; i--){
      for(int j=i; j<rows; j++){
        System.out.print(" ");
      }	
      for(int j = 1; j <= i; j++){
        System.out.print(i);
      }	           
      System.out.println();	           
    }
    for(int i=2; i<=rows; i++){
      for(int j=rows; j>i; j--){
        System.out.print(" ");
      }
      for(int j=1; j<=i; j++){
        System.out.print(i);
      }
      System.out.println();
    }     
  }
}

Java pattern program – Pattern 12

8 8 8 8 8 8 8 8 
 7 7 7 7 7 7 7 
  6 6 6 6 6 6 
   5 5 5 5 5 
    4 4 4 4 
     3 3 3 
      2 2 
       1 
      2 2 
     3 3 3 
    4 4 4 4 
   5 5 5 5 5 
  6 6 6 6 6 6 
 7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
    sc.close();
  }
	
  private static void displayPyramidPattern(int rows){
    //for upper pyramid
    for(int i=rows; i>=1; i--){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print number 
      for(int k=0; k<i; k++){
        System.out.print(i + " ");
      }
      System.out.println();           
    }  
    //for lower pyramid
    for(int i=2; i<=rows; i++){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print number 
      for(int k=0; k<i; k++){
        System.out.print(i + " ");
      }
      System.out.println();           
    }     
  }
}

Java pattern program – Pattern 13

12345654321
 123454321
  1234321
   12321
    121
     1
    121
   12321
  1234321
 123454321
12345654321
public class PatternProgram {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows in the pyramid (1-9)- ");
    int rows = sc.nextInt();
    displayPyramidPattern(rows);
    sc.close();
  }
	
  private static void displayPyramidPattern(int rows){
    for(int i=rows; i>=1; i--){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print incrementing part
      for(int k=1; k<i; k++){
        System.out.print(k);
      }
      // print decrementing part
      for(int k=i; k>=1; k--){
        System.out.print(k);
      }
      System.out.println();     
    }
    for(int i=2; i<=rows; i++){
      // print correct number of spaces 
      // in each row
      for(int j=0; j<rows-i; j++){
        System.out.print(" ");
      }
      // print incrementing part
      for(int k=1; k<i; k++){
        System.out.print(k);
      }
      // print decrementing part
      for(int k=i; k>=1; k--){
        System.out.print(k);
      }
      System.out.println();     
    }     
  }
}

That's all for the topic Java Programs For Displaying Patterns. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment