Pattern Program in Java
Overview
Pattern Programs in Java are the problems where we have to observe and recreate a similar pattern and print it on screen. This helps us improve our critical thinking abilities and also helps us improve our implementation and problem-solving skills. These problems are a critical part of Technical Interviews. Generally, we deal with the Pattern Program in Java, with loops.
Introduction to Pattern Programs in Java
Now, why do we need to learn this topic separately, we can just copy and paste the pattern?
Besides defying the whole purpose of the Pattern Programs, Copying can only be done for constant size. For Pattern Programs in Java, we take the size as an input.
Pattern Programs in Java are generally solved with nested loops, mostly we will be using for loops because of their convenience, but while and do-while loops can also be used.
We can divide the Pattern Program in Java, into 3 basic categories:
- Star Patterns
- Number Patterns
- Character Patterns
Star Pattern Programs in Java
Star Pattern Program in Java generally asks us to print * (asterisk) in different shapes and forms. Most commonly we see a mixture of squares, rectangles, and triangles in these patterns. Let's understand it with the help of an example of a star pattern.
Example 1: Right-angled Triangle of * (Left)
Pattern:
Observation:
Here we can observe that we need a nested pair of loops, to print columns and rows.
Let's discuss the conventions that we (generally) follow:
- As we can see from the pattern, we will be requiring nested loops. We can use while, do-while, or for loops for this purpose, for simplicity, we will be using for loops.
- We will be using i,j,k,.. as loop variables for generalizing. i is the outermost loop, and j,k... are inside it.
- Since 'i' is the outermost loop, it deals with rows.
- And since 'j' is the inner loop, it deals with columns.
- We always try to break a big pattern into smaller ones.
- If there are some spaces in the pattern, then we replace them with '-' or any other character in rough. It will help us to correctly solve the pattern.
Explanation: Here, we can observe that the 1st row has 1 star, the 2nd has 2 stars, and ith row has i stars. Therefore, we can print i stars in every row.
We will achieve this by running the inner ('j') loop 'i' times.
Our loop structure will look like this:
Code:
Example 2 : Print Square Grid of Stars
Pattern:
Observation:
Here we are required to print a square of '*' of size n×n. So for doing so, we will use nested for loops. We want to leave a line after every 'n' stars.
Explanation:
We have to print n stars in every row. So our outer loop will iterate 'n' times and the inner loop will also iterate 'n' times.
Example 3 : Inverted Right Angled Triangle of * (left)
Pattern:
Observation:
Here, we can observe that the stars are decreasing with an increase in the value of 'i'.
Explanation:
We want to print (n-i+1) stars in each row. So our outer loop will iterate ‘n’ times and the inner loop will iterate (n-i+1).
Example 4 : Right Angled Triangle of * (Right)
Pattern:
Observation: This pattern is very similar to Example 1. The difference is the space part. We need to print the space triangle as well. To deal with spaces, let's first replace them with '-'.
Now we can observe that the space triangle is similar to Example 3.
Explanation:
We want to club 2 triangles side by side (Space triangle, and Star Triangle). Therefore the rows do not change. So we want will have to limit the outer loops to 1.
So our outer loop will iterate n times. For inner loops, we will need 2 of them for the 2 triangles.
The spaces loop will be very similar to Example 3 (The only difference is it is printing spaces). It will iterate (n-i) times, since we want n-1 spaces in 1st row, n-2 in 2nd row,.. and n-n (=0) in the last row.
Star loop is very similar to Example 1. It will iterate i times.
Example 5 : Inverted Right angled Triangle of * (Right)
Pattern:
Observation:
This pattern looks similar to Example 3 in terms of the number of stars in each row. The difference is the space part. We need to print the space triangle as well.
To deal with spaces, let's first replace them with '-'.
Now we can observe that the space triangle is similar to Example 1. Let's divide it into smaller patterns.
Explanation: We want to club 2 triangles side by side (Space triangle, and Star Triangle). Therefore the rows do not change. So we want will have to limit the outer loops to 1.
So our outer loop will iterate n times. For inner loops, we will need 2 of them for the 2 triangles.
The spaces loop will be very similar to Example 1. We will iterate it till i-1 since the 1st row has 0 spaces, the 2nd row has 1 space,.. ith row has i-1 spaces.
Star loop is very similar to Example 3. It will iterate n-i+1 times.
Example 6 : Arrowhead (Left)
Pattern:
Observation: This pattern is a combination of patterns of Example 1 and Example 3. We can divide it like this:
Explanation:
Since we don't have to print these 2 patterns side by side, and instead, we need to print it one after the other, we can use 2 nested loops. Nested Loop 1 for 1st triangle, similar to Example 1, of size n.
Nested Loop 2 for 2nd triangle, similar to Example 3, of size n-1, since 1 row is overlapping.
Example 7 : Arrowhead (Right)
Pattern:
Observation: It looks like patterns we have done earlier. It will be more clear once we replace spaces with '-'.
We can see that it is a mixture of 4 triangles, where all smaller triangles are similar to Example 1 and Example 3. Now we can divide this pattern into 2 or 4 patterns like this:
Explanation: We can see that we need to print 4 triangles, 1 and 2 side by side, and 3 and 4 side by side. Therefore we will need 2 pairs of nested loops.
1st nested loop will iterate for n times and print 2 triangles:
- a space triangle of size n-1, similar to Example 3.
- a star triangle of size n, similar to Example 1.
2nd nested loop will iterate for n-1 times and print 2 triangles:
- a space triangle of size n-1, similar to Example 1.
- a star triangle of size n-1, similar to Example 3. (Size is n-1 Since 1 row is overlapping)
Example 8 : Valley
Pattern:
Observation: It looks like star triangles and 1 space triangle. For understanding the space triangle, let's replace spaces with '-'.
So we can divide the pattern into 3 patterns:
Explanation: Since we want to print the triangles side by side, we will have to use 1 outer loop and then divide the pattern into 3 parts.
3 parts:
Both the star triangles are similar to Example 1, of size n.
The space triangle is similar to Example 3 but we need to print 2 spaces instead of 1. This can easily be observed because we want: 2*(n-1) spaces in 1st row, 2*(n-2) spaces in 2nd row, 2*(n-i) spaces in ith row. Therefore, the size of the space triangle is n-1.
Example 9 : Inverted Valley
Pattern:
Observation: This pattern is the reverse of the last pattern. And so it looks like we can divide into 2 star triangles, and 1 space triangle. For understanding the space triangle, let's replace spaces with '-'.
So we can divide the pattern into 3 patterns:
Explanation: Since we want to print all the 3 triangles side by side, we will have to use only 1 outer loop.
Since we have only 1 outer loop, and this pattern can be easily achieved by inverting the last pattern, we can just reverse the outer for loop, i.e.,
And we can also deal this as a new problem.
3 Parts:
Both the star triangles are similar to Example 3, of size n. Iteration for n-i+1 times.
The space triangle is similar to Example 1 but we need to print 2 spaces instead of 1. This can easily be observed because we want: 0 spaces in 1st row, 2 spaces in 2nd row, 2*(i-1) spaces in ith row. Therefore, the size of Space triangle is n-1.
Example 10 : Hollow Diamond
Pattern:
Observation: This looks like a combination of last 2 patterns. Let's replace all the spaces with '-', to observe the pattern.
Now, let's try to break it down into simpler patterns.
While doing so, we can see that this is nothing but a combination of previous 2 patterns.
Explanation: Since we want to print the 2 patterns one after the other, we will use 2 pairs of nested loops. One from Example 8 and other from Example 9.
The spaces are doubled.
Example 11 : Spaced Pyramid
Pattern:
Observation: This shares similarities with Example 1, since we have 1 star in 1st row, 2 stars in 2nd row, and i stars in ith row. To understand spaces, let's replace all the spaces with '-'.
We can see that we need to print a space triangle, similar to Example 3. And we also need to print stars with a space attached.
Now, lets try to break it into simpler patterns.
Explanation: Since space and star triangles are sides by side, we will limit the number of outer loops to 1.
Space loop will be similar to Example 3, of size n-1.
Triangle loop will be similar to Example 1, of size n. There is an extra space with each '*'.
Example 12 : Inverted Spaced Pyramid
Pattern:
Observation: This pattern is just the inverted version of last pattern. We can divide it into 2 triangles. Let's replace all the spaces with '-'.
Now, lets try to break it into simpler patterns.
Explanation: Since we have only 1 outer loop (as both the triangles are printed side by side), we can invert the outer loop like this:
We can also approach it by breaking into smaller patterns.
Space loop will be similar to Example 1, of size n-1.
Triangle loop will be similar to Example 3, of size n.
There is an extra space with each '*'.
Example 13 : Hourglass
Pattern:
Observation: We can see that this pattern is a combination of last to patterns. Let's replace all the spaces with '-'.
Now, let's try to break it into simpler patterns.
Explanation: We can see that in this pattern, we are printing Example 11 and Example 12, one after other. Therefore, we will use 2 pairs of nested loops for the 2 parts.
Loop 1 will be similar to Example 11. Loop 2 will be similar to Example 12
There is an extra space with each '*'.
Example 14 : Spaced Triangle
Pattern:
Observation: In this pattern, stars are related to rows as: 1st row has 1 star, 2nd row has 3 stars, ith row has (2i - 1) stars.
Let's replace all the spaces with '-'.
Let's divide into 2 triangles.
Explanation: In this pattern we have to deal with 2 triangles.
Space triangle is of size (n-1), and is printing 2 spaces. It is similar to Example 3, with difference being the number of spaces.
Star triangle is of size n, it is printing "*" with a space. It is iterating for (2i-1) times as discussed before.
Example 15 : Inverted Spaced Triangle
Pattern:
Observation: This pattern is the reversed version of the last pattern. Let's replace all the spaces with '-'.
Explanation: We can just reverse the outer loop and it will do the trick. We can also divide it into 2 triangles. Since we want to print them side by side, we will use only 1 outer loop.
We need to print:
A space triangle of size (i-1), similar to Example 1, with the difference being in the number of spaces.
A star triangle of size n. Since we want to print the reverse triangle, we can come up with many relations. One such relation is to print 2(n-i+1)-1* stars in every column.
Takeaway: (n-i+1) is very common while printing a pattern that is decreasing.
Example 16 : Diamond
Pattern:
Observation: This pattern is a combination of last 2 patterns. Let's replace all the spaces with '-'
We can see that this pattern is nothing but combination of last 2 patterns.
Explanation: We will use 2 nested loops for 2 parts. Loop 1 similar to Example 14, of size n. Loop 2 similar to Example 15, of size n-1. It is because 1 row is overlapping.
Example 17 : Hollow Rectangle
Pattern:
Observation: We can observe that we want to print a n*n matrix, with some constraints. We don't want to print any stars in the interior. Let's replace all the spaces by '-'
We can see that we don't want to print a star anywhere except the border.
Explanation: So we can use an if for checking if "i is 1" or "j is 1" or "i is n" or "j is n", then only we will print the star.
The size of the pattern is n*n.
Example 18 : Hollow Right Angled Triangle (Left)
Pattern:
Observation: This pattern is similar to Example 1. But, here we want to print stars only in borders. Let's replace all the spaces with '-'.
Explanation: We can see that stars are just in the borders. So again, we will use an if statement.
Loops are very similar to Example 1. Outer loop will iterate n times, and the Inner loop will iterate i times.
Example 19 : Hollow Right Angled Triangle (Right)
Pattern:
Observation: This pattern is a combination of Example 18 and Example 3. Let's replace all the spaces with '-'.
We can divide it like this:
We can see that stars are just in the borders. So again, we will use an if statement. But here we also have to print a space triangle.
Explanation:
Since the space triangles and star triangles are printed side by side, we will limit the number of outer loops to 1. We will print a space triangle, similar to Example 3, of size n-1.
The space loop will iterate for n-i times.
The star part is similar to Example 18.
Example 20 : Hollow Arrowhead (Left)
Pattern:
Observation: It looks like that one part of this pattern is very similar to Example 18. To understand the pattern properly, let's replace all the spaces with '-'.
We can see that stars are just in the borders. Let's divide it into smaller patterns.
Explanation: We will use 2 pairs of nested loops to display the 2 parts.
Size of the upper triangle is n. Since there are i characters in ith row, we will iterate the inner loop for i times, and the outer loop for n times. For spaces, we will have to keep a check, if j is 1 or j is I, then we will print stars, else, print spaces.
For the lower triangle, we can see that we have 1 row overlapping, so we will print a decreasing triangle, similar to Example 3. Outer loop will iterate for n-1 times. We also have to take care of the spaces. So, j loop will iterate for n-1 times, and if j is 1 or j is n-i, we will print stars, else, print spaces.
Example 21 : Hollow Arrowhead (Right)
Pattern:
Observation: This pattern looks like a combination of the last pattern and the spaces triangles look like something we have delved into before. To better understand the spaces, let's replace all the spaces with '-'.
As we can see that space triangles are very similar to Example 1 and Example 3. Let's divide the pattern into smaller patterns.
Explanation: Just like the Example 20, we will divide this pattern into 2 halves, upper and lower. With the upper half, we will have to print a space triangle similar to Example 3, of size n-1. For the lower half, we will have to print a space triangle similar to Example 1, of size n-1.
Example 22 : Arrow (Left)
Pattern:
Observation: This pattern is very similar to Example 20, the difference being the row of stars in middle. As we do in every pattern, let's replace all the spaces with '-'.
To print the pattern, we can divide it into 2 halves (upper and lower).
Explanation: We will use 2 pairs of nested loops for the 2 halves. Let's take reference to Example 20, and solve this pattern. We just want to print a row of stars in the last row of the upper triangle. We will achieve it with the help of it. We will check that if j is 1, or j is i or i is n, print star, else print space.
Example 23 : Arrow (Right)
Pattern:
Observation: This pattern is very similar to the last pattern, but differs in spaces. For understanding space, let's replace all the spaces with '-'.
Let's break this down into smaller patterns.
Explanation: This pattern is very similar to the last pattern, we just have to take care of 2 space triangles.
We will use 2 pairs of nested loops for the 2 halves.
For the upper half, the space triangle is of size n-1 and it is very similar to Example 3.
And for the lower half, the space triangle is of size n-1 and it is very similar to Example 1.
Since the space triangles and the pattern are side by side, we don't have to make separate nested loops, we will club into the 2 nested loops used for the upper and lower halves.
Number Pattern Programs in Java
Now that we have seen the examples of star patterns, let's play with numbers.
In Number Pattern Programs in Java, we are given a pattern that has some number series in it, and we need to print it in a specific way. It's just that we need to change the above logic so that it prints numbers instead of * this this.
Example 24 : Grid of numbers (Increasing row wise)
Pattern:
Observation: This looks like a square matrix. For the numbers, we can observe that numbers in each row are same, and are increasing if we go down a row.
Explanation: Size of the matrix is n*n. We will use a pair of nested loops to print this pattern. The outer loop and inner loop will iterate for n times each.
Here we want to print the row number in every row. We can do this by printing the value of 'i'.
Example 25 : Right Angled Triangle (Increasing column-wise)
Pattern:
Observation: This pattern looks similar to Example 25. The difference is that here we want to print the triangle. We can refer to Example 1 to do that.
Explanation: We will print 'j' in every row, and we will bound j till i, as done in patterns before.
You can refer Number Pattern Programs in Java to get a better understanding of different number pattern logics.
Character Pattern Programs in Java
Character Pattern Programs in Java are very similar to Star Pattern Programs in Java, but they have sequences like Number Pattern Programs in Java.
A simple character problem would be to print the alphabet in Uppercase. We can easily do this, Just by knowing the ASCII code of 'A' and iterating till 'Z'.
ASCII codes:
- A-Z (Upper case) : 65 to 90.
- a-z (Lower case) : 97 to 122.
- ' ' (blank space) : 32
We can use explicit type conversion to convert the ASCII values to characters.
Output:
Remember: if we ever forget the ASCII codes, we can use the character instead. (Example : 'a' instead of 97). We will use this method for all the Character Pattern Programs in Java that we will solve.
Conclusion
- Pattern Programs in Java, help us improve our critical thinking and code implementation abilities.
- While solving Pattern Programs in Java, we always need to think about the loops.
- Generally, we print something with i variable when the pattern is similar row wise.
- Generally, we print something with j variable when the pattern is similar column wise.
- While implementing, we can use '-' (or any other character) instead of spaces, to count and correctly solve the problem.
- (n-i+1) is common while printing a pattern which is decreasing.