DEV Community

Cover image for Pattern program in Java
Satish Chandra Gupta
Satish Chandra Gupta

Posted on • Edited on

Pattern program in Java

Creating different pattern programs boost your learning speed and understanding of a programming language.

It helps you make a good grip especially over loops.

After completing this article you will have a good understanding of Java and the control flow of loops in Java.

We are going to create different pattern programs in java originally posted at Tutorials Tonight.

Here we go:🚚

1. Square Pattern

*****
*****
*****
*****
Enter fullscreen mode Exit fullscreen mode

You simply need to loop create a nested loop that run for 'n' times (here 5) and print star in the internal loop.
At the end of the internal loop print a new line.

public class squarePattern {
  public static void main(String[] args) {

    // size of the square
    int size = 5;
    // outer loop
    for (int i = 0; i < size; i++) {
      // inner loop
      for (int j = 0; j < size; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Hollow square Pattern

*****
*   *
*   *
*   *
*****
Enter fullscreen mode Exit fullscreen mode

Hollow square pattern is a bit tricky you need to control internal space and print stars only at boundaries.

public class hollowSquare {
  public static void main(String[] args) {

    // size of the square
    int size = 5;
    // outer loop
    for (int i = 0; i < size; i++) {
      // inner loop
      for (int j = 0; j < size; j++) {
        // print only star in first and last row
        if (i == 0 || i == size - 1) {
          System.out.print("*");
        }
        else {
          // print star only at first and last position row
          if (j == 0 || j == size - 1) {
            System.out.print("*");
          }
          else {
            System.out.print(" ");
          }
        }
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Left triangle Pattern

*
**
***
****
*****
Enter fullscreen mode Exit fullscreen mode

Create a nested loop and print the number of stars in each internal loop as the number of times the external loop has executed and print a new line after each internal loop.

public class leftTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    // loop to print the pattern
    for (int i = 0; i < size; i++) {
      // print column
      for (int j = 0; j <= i; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Right triangle Pattern

    *
   **
  ***
 ****
*****
Enter fullscreen mode Exit fullscreen mode

This pattern is a bit tricky as you have to manage both stars and spaces.

You have to use 2 internal loops first one will print space for (size - i), where i is the number of times the external loop has been executed. And the second loop will print the star for i number of times.

public class rightTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    // loop to print the pattern
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 1; j < size - i; j++) {
        System.out.print(" ");
      }
      // print stars
      for (int k = 0; k <= i; k++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

5. Hollow left triangle Pattern

*
**
* *
*  *
*   *
******
Enter fullscreen mode Exit fullscreen mode

You can see the pattern above. It is hollow from the inside. You have to control your loop such as in each row it prints stars only at the first and last position.

Here is the complete code for this in java.

public class hollowTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    for (int i = 1; i <= size; i++) {
      for (int j = 0; j < i; j++) {
        // not last row
        if (i != size) {
          if (j == 0 || j == i - 1) {
            System.out.print("*");
          } else {
            System.out.print(" ");
          }
        }
        // last row
        else {
          System.out.print("*");
        }
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

6. Pyramid Pattern

    *
   ***
  *****
 *******
*********
Enter fullscreen mode Exit fullscreen mode

The pyramid is quite a famous pattern. You can see the pattern above. To create this first print space to shift stars to the right so that it makes a pyramid shape.

Here is the complete code for the pyramid in java.

public class pyramid {
  // pyramid star pattern
  public static void main(String[] args) {

    int size = 5;
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < size - i - 1; j++) {
        System.out.print(" ");
      }
      // print stars
      for (int k = 0; k < 2 * i + 1; k++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

7. Reverse pyramid Pattern

*********
 *******
  *****
   ***
    *
Enter fullscreen mode Exit fullscreen mode

Reverse pyramid pattern is nothing but pyramid pattern rotated 180 degrees.

To create these print spaces in increasing order and stars in decreasing order in number.

public class reversePyramid {
  public static void main(String[] args) {
    // size of the pyramid
    int size = 5;
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // print stars
      for (int k = 0; k < 2 * (size - i) - 1; k++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

8. Diamond star Pattern

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Enter fullscreen mode Exit fullscreen mode

You can see above what a diamond pattern looks like.

If you observe carefully it is made up of pyramid and reverse pyramid pattern.

This is quite complex pattern. The complete code for this is given below:

public class diamond {
  public static void main(String[] args) {
    int size = 5;
    // upside pyramid
    for (int i = 1; i <= size; i++) {
      // printing spaces
      for (int j = size; j > i; j--) {
        System.out.print(" ");
      }
      // printing star
      for (int k = 0; k < i * 2 - 1; k++) {
        System.out.print("*");
      }
      System.out.println();
    }
    // downside pyramid
    for (int i = 1; i <= size - 1; i++) {
      // printing spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // printing star
      for (int k = (size - i) * 2 - 1; k > 0; k--) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

9. Hourglass star Pattern

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********
Enter fullscreen mode Exit fullscreen mode

You can see above what an hourglass pattern looks like.

If you observe carefully it is made up of reverse pyramid and pyramid pattern.

The complete code for hourglass is given below:

public class hourGlass {
  public static void main(String[] args) {
    int size = 5;
    // reversed pyramid star pattern
    for (int i = 0; i < size; i++) {
      // printing spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // printing star
      for (int k = 0; k < (size - i) * 2 - 1; k++) {
        System.out.print("*");
      }
      System.out.println();
    }
    // pyramid star pattern
    for (int i = 2; i <= size; i++) {
      // printing spaces
      for (int j = size; j > i; j--) {
        System.out.print(" ");
      }
      // printing star
      for (int k = 0; k < i * 2 - 1; k++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

10. Heart star Pattern

 ***   ***
***** *****
***********
 *********
  *******
   *****
    ***
     *
Enter fullscreen mode Exit fullscreen mode

Heart pattern is really complex to create.

Here java program for heart pattern using stars.

public class heart {
  public static void main(String[] args) {
    // heart star pattern
    int size = 6;

    for (int i = size / 2; i < size; i += 2) {
      // print first spaces
      for (int j = 1; j < size - i; j += 2) {
        System.out.print(" ");
      }
      // print first stars
      for (int j = 1; j < i + 1; j++) {
        System.out.print("*");
      }
      // print second spaces
      for (int j = 1; j < size - i + 1; j++) {
        System.out.print(" ");
      }
      // print second stars
      for (int j = 1; j < i + 1; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
    // lower part
    // inverted pyramid
    for (int i = size; i > 0; i--) {
      for (int j = 0; j < size - i; j++) {
        System.out.print(" ");
      }
      for (int j = 1; j < i * 2; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)