Tuesday, 31 May 2016

How to display pyramid patterns in Java - Part1

Writing a program to display a pyramid of given 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 program in Java to display those 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).

Pattern 1

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

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

Logic is to have a loop that will itearte depending on the rows that are needed. Then there is a nested loop to display spaces. There is another nested loop to dispaly 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();
            
        }
            
    }

}

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

}

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

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

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

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

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!


Related Topics

  1. Armstrong number
  2. Count total number of times each character appears in a String
  3. Count number of words in a String
  4. Print odd-even numbers using threads and wait-notify
  5. How to reverse a string in Java

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment