Create any pattern easily with simple steps in Java

Why are patterns important?

Patterns are important because it help programmers to enhance their problem-solving skills and gain a deeper understanding of coding architecture. This tutorial will give you a solid foundation to start building all kinds of patterns.

If you are new to programming or data structure algorithms then make sure to focus on pattern making as it is going to improve your thought process and logic building.

Now that we know why patterns are important, let’s look into how we can solve them.

Also check: Java program to print alphabetical pattern

Pre-requisites:

  • Basic knowledge of loops
  • Basic knowledge of 2D arrays

How to approach :

There are a few key steps you need to keep in mind to solve these problems.

1.  Finding the structure

  • Always run the outer loop for as many times as the number of rows in the pattern.
  • Next, for each row, identify the number of columns and the types of elements(numbers, special characters or alphabets) in it and print accordingly.

2. Printing the pattern

  • If the elements of the patterns are characters or symbols then store them in a variable and print accordingly.
  • If the elements of the patterns are number then check if you can use the row and column numbers to print the number or have to use a separate variable.
  • You can also try creating a formula relating rows and columns that might create the pattern.

I  recommend you run the following codes on your machine instead of using online compilers.

Let’s learn this using an example pattern.

Example 1:

*
* *
* * *
* * * *

First, we will run the outer-loop for as many times as the number of rows, which is 4.
Next, we can see that for each line, the number of rows = number of columns so we will have to run the inner loop once for each iteration of the outer loop.
Then, we will print the element of the pattern each time the inner loop is executed.
Finally, we will add a new line every time a row is printed.

Code:

class Pattern1 {
    public static void main(String[] args) {
      for(int row=1;row<=4;row++)//Outer loop
      {
          for(int col=1;col<=row;col++)//Inner loop
          {
              System.out.print("* ");
          }
          System.out.println();
      }
    }
}

Example 2:

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

Again, first, we will run the outer loop for as many times as the number of rows, which is 5.
Next, we can see that there are 5 columns in the first row and the number of columns decreases by one every time a new row is printed. So, we will run the inner loop from 5 till the row number.
Then, we will print the element of the pattern for each time the inner loop is executed.
Finally, we will add a new line every time a row is printed.

Code:

class Pattern2 {
    public static void main(String[] args) {
        for(int row=1;row<=5;row++)
        {
            for(int col=5;col>=row;col--)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Thank you for learning from CodeSpeedy. If you have any doubts, feel free to ask them in the comments section below. For more example patterns, check my other posts on patterns.

You may also read:

How to create dynamic 2D array in Java.

2 responses to “Create any pattern easily with simple steps in Java”

  1. Amrita Kumari says:

    In this code java.lang.ClassNotFoundException is show while excuting the code.

Leave a Reply

Your email address will not be published. Required fields are marked *