Saturday, April 6, 2019

How to Display Pyramid Patterns in Java - Part1

Writing a Java program to display a pyramid pattern is a good way to learn about nested loops. The pattern may contain numbers or any special symbol. So let's see some of the patterns and how to write a Java program to display those number or symbol patterns.

If you have noticed in most of the patterns one common thing is; it narrows down at the top (or bottom in case of reverse pyramid) for that you have to print that much spaces before printing the number(s).

Java code for pyramid of numbers - Pattern 1

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

In this pattern display count of a number is equal to the number in that row.

Logic is to have a loop that will iterate depending on the rows that are needed. Then there is a nested loop to display spaces. There is another nested loop to display the number.

import java.util.Scanner;

public class PatternsDemo {

    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);

    }
    
    private static void printPattern(int num){
        for(int i = 1; i <= num; i++){
            // this loop will print the spaces after which the
            // number has to be printed
            for(int j = 0; j < num - i; j++){
                System.out.print(" ");
            }
            // this loop will print the number
            for(int k = 0; k < i; k++){
                System.out.print(i + " ");
            }
            System.out.println();           
        }           
    }
}

Java code for pyramid of stars - Pattern 2

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

The logic for this pattern is same as above, only change is instead of number asterisk (*) has to be displayed.

import java.util.Scanner;

public class PatternsDemo {

    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);

    }
    
    private static void printPattern(int num){
        for(int i = 1; i <= num; i++){
            // this loop will print the spaces after which the
            // number has to be printed
            for(int j = 0; j < num - i; j++){
                System.out.print(" ");
            }
            // this loop will print the number
            for(int k = 0; k < i; k++){
                System.out.print("* ");
            }
            System.out.println();           
        }          
    }
}

Java code for number pattern - Pattern 3

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

In this pyramid pattern instead of displaying the same number, numbers are displayed in ascending order in each row starting from 1.

import java.util.Scanner;

public class PatternsDemo {

    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);

    }
    
    private static void printPattern(int num){
        for(int i = 1; i <= num; i++){
            // this loop will print the spaces after which the
            // number has to be printed
            for(int j = 0; j < num - i; j++){
                System.out.print(" ");
            }
            // this loop will print the number
            for(int k = 1; k < i; k++){
                System.out.print(k + " ");
            }
            System.out.println();
            
        }            
    }
}

Java code for half pyramid pattern - 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
1 2 3 4 5 6 7 8 9

This pattern is same as the pattern above minus the spaces. So the loop that prints the spaces is not needed.

import java.util.Scanner;

public class PatternsDemo {

    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);

    }
    
    private static void printPattern(int num){
        for(int i = 1; i <= num; i++){
            
            // this loop will print the number
            for(int j = 1; j <= i; j++){
                System.out.print(j + " ");
            }
            System.out.println();
            
        }            
    }
}

Java code for number pattern - Pattern 5

        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321

Here rather than same number or in ascending order numbers are displayed in each row in both ascending and then descending order. That is why there is one more nested loop.

import java.util.Scanner;

public class PatternsDemo {

    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);

    }
    
    private static void printPattern(int num){
        for(int i = 1; i <= num; i++){
            // this loop will print the spaces after which the
            // number has to be printed
            for(int j = 0; j < num - i; j++){
                System.out.print(" ");
            }
            // this loop will print the number ascending part
            for(int k = 1; k < i; k++){
                System.out.print(k);
            }
            // this loop will print the number descending part
            for(int l = i; l >=1; l--){
                System.out.print(l);
            }
            System.out.println();            
        }           
    }
}

Java code for reverse pyramid pattern - Pattern 6

12345678987654321
 123456787654321
  1234567654321
   12345654321
    123454321
     1234321
      12321
       121
        1

This is the reverse pyramid which follows the same pattern as above but upside down.

import java.util.Scanner;

public class PatternsDemo {

    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);

    }
    
    private static void printPattern(int num){
        for(int i = num; i >= 1; i--){
            // this loop will print the spaces after which the
            // number has to be printed
            for(int j = 1; j <= num - i; j++){
                System.out.print(" ");
            }
            // this loop will print the number ascending part
            for(int k = 1; k < i; k++){
                System.out.print(k);
            }
            // this loop will print the number descending part
            for(int l = i; l >=1; l--){
                System.out.print(l);
            }            
            System.out.println();           
        }            
    }
}

Java code for pattern (Floyds Triangle) - Pattern 7

Triangle with consecutive numbers.

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
public class PatternsDemo {
    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);
    }
    
    private static void printPattern(int num){
        int number = 1;
        for(int i = 1; i <= num; i++){                
            for(int j = 0; j < i; j++){
                System.out.print(number++ + " ");
            }
            System.out.println();               
        } 
    }
}

Java code for pattern - Pattern 8

1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
public class PatternsDemo {
    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 noOfRows = sc.nextInt();
        // calling method
        printPattern(noOfRows);
    }
    
    private static void printPattern(int num){
        for(int i = 0; i < num; i++){
            
            // this loop prints the number
            for(int j = 1; j <= num - i; j++){
                System.out.print(j + " ");
            }
            System.out.println();            
        }    
    }
}

That's all for this topic How to Display Pyramid Patterns in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Armstrong Number or Not Java Program
  2. How to Swap or Exchange Two Numbers Without Using Any Temporary Variable - Java Program
  3. Checking Number Prime or Not - Java Program
  4. Fibonacci Series Program in Java
  5. How to Find The Longest Palindrome in The Given String

You may also like-

  1. How to Add Double Quotes to a String - Java Program
  2. Matrix Multiplication - Java Program
  3. How to Convert Date to String in Java
  4. Array in Java
  5. Polymorphism in Java
  6. varargs in Java
  7. Difference Between ReentrantLock and Synchronized in Java
  8. What is Dependency Injection in Spring