Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Create Pyramid and Pattern


In this article, we will understand how to create pyramid and pattern. The pattern is formed by using multiple for-loops and print statements.

Below is a demonstration of the same −

Input

Suppose our input is −

The number of rows is defined as 8

Output

The desired output would be −

The pyramid pattern :
             1
           2 3 2
         3 4 5 4 3
        4 5 6 7 6 5 4
     5 6 7 8 9 8 7 6 5
   6 7 8 9 10 11 10 9 8 7 6
 7 8 9 10 11 12 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 14 13 12 11 10 9 8

Algorithm

Step 1 - START
Step 2 - Declare six integer values namely my_input, k, i, my_count, my_temp and my_spaces.
Step 3 - Read the required values from the user/ define the values
Step 4 - Assign value of 0 to ‘k’, my_count and my_temp
Step 5 - We iterate through two nested 'for' loops to get space between the characters.
Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the value of (i + k - 2 * my_temp).
Step 7 - Now, print a newline to get the specific number of characters in the subsequent lines.
Step 8 - Display the result
Step 9 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool Java Program to Create Pyramid and Pattern.

import java.util.Scanner;
public class Pyramid {
   public static void main(String[] args) {
      int my_input, k, i, my_count, my_temp, my_spaces;
      my_input = 8;
      k = 0;
      my_count = 0;
      my_temp = 0;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows: ");
      my_input = my_scanner.nextInt();
      System.out.println("The pyramid pattern : ");
      for ( i = 1; i <= my_input; ++i) {
         for (my_spaces = 1; my_spaces <= my_input - i; ++my_spaces) {
            System.out.print(" ");
            ++my_count;
         }
         while (k != 2 * i - 1) {
            if (my_count <= my_input - 1) {
               System.out.print((i + k) + " ");
               ++my_count;
             } else {
                 ++my_temp;
                 System.out.print((i + k - 2 * my_temp) + " ");
             }
             ++k;
          }
          my_temp = my_count = k = 0;
          System.out.println();
     }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number of rows: 8
The pyramid pattern :
             1
           2 3 2
         3 4 5 4 3
        4 5 6 7 6 5 4
      5 6 7 8 9 8 7 6 5
    6 7 8 9 10 11 10 9 8 7 6
  7 8 9 10 11 12 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 14 13 12 11 10 9 8

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Pyramid {
   public static void main(String[] args) {
   int my_input, k, i, my_count, my_temp, my_spaces;
      my_input = 8;
      k = 0;
      my_count = 0;
      my_temp = 0;
      System.out.println("The number of rows is defined as " +my_input);
      System.out.println("The pyramid pattern : ");
      for ( i = 1; i <= my_input; ++i) {
         for (my_spaces = 1; my_spaces <= my_input - i; ++my_spaces) {
            System.out.print(" ");
            ++my_count;
         }
         while (k != 2 * i - 1) {
            if (my_count <= my_input - 1) {
               System.out.print((i + k) + " ");
               ++my_count;
            } else {
               ++my_temp;
               System.out.print((i + k - 2 * my_temp) + " ");
            }
            ++k;
          }
          my_temp = my_count = k = 0;
          System.out.println();
      }
   }
}

Output

The number of rows is defined as 8
The pyramid pattern :
                  1
                2 3 2
             3 4 5 4 3
           4 5 6 7 6 5 4
         5 6 7 8 9 8 7 6 5
     6 7 8 9 10 11 10 9 8 7 6
  7 8 9 10 11 12 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 14 13 12 11 10 9 8