Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have a deep knowledge of the Java loop, such as for loop do-while loop. In this section, we will learn how to print a pattern in Java.
We have classified the Java pattern program into three categories:
- May 07, 2020 Star Patterns in Java. First, let us begin with the basic and the commonly asked pattern program in Java i.e Pyramid. Pyramid Program. Let’s write the java code to understand this pattern better. Right Triangle Star Pattern. System.out.println ; 3.
- For example, 4th row contains numbers from 1 to 4. We will use two for loops to print right triangle number pattern. For a right triangle number pattern of N rows, outer for loop will iterate N time (from i = 1 to N). Each iteration of outer loop will print one row of the pattern. The inner for loop will print all elements of a row.
Notice that every row has 2.r-1 numbers. So we will have (rows-i).2 whitespaces before we print any number. Then the numbers are starting from 1 to “i” and then to 1 again. Our logic to print the numbers would require two for-loops to achieve this. Pyramid Pattern 5 Program in Java. Apr 09, 2021 This Java Pattern Programs article covers almost every possible type of pattern programs that will give you a better understanding of the logic to decode the pattern and become capable of building one in your interview. We will deal with 25 different types of Java Pattern Programs through the following docket. Pattern 1 /.Star Pattern 1.
- Start Pattern
- Number Pattern
- Character Pattern
Before moving to the pattern programs, let's see the approach.
Whenever you design logic for a pattern program, first draw that pattern in the blocks, as we have shown in the following image. The figure presents a clear look of the pattern.
Each pattern program has two or more than two loops. The number of the loop depends on the complexity of pattern or logic. The first for loop works for the row and the second loop works for the column. In the pattern programs, Java for loop is widely used.
In the above pattern, the row is denoted by i and the column is denoted by j. We see that the first row prints only a star. The second-row prints two stars, and so on. The colored blocks print the spaces.
Let's create the logic for the pattern, give above. In the following code snippet, we are starting row and column value from 0. We can also start it from 1, it's your choice.
In the above code snippet, the first for loop is for row and the second for loop for columns.
Let's see the execution of the code step by step, for n=4 (the number of rows we want to print).
Iteration 1:
The first print statement prints a star at the first row and the second println statement prints the spaces and throws the cursor at the next line.
Now the value of i and j is increased to 1.
Iteration 2:
The first print statement prints two stars at the second row and the second println statement prints the spaces and throws the cursor at the next line.
Now the value of i and j is increased to 2.
Iteration 3:
The first print statement prints three stars at the third row and the second println statement prints the spaces and throws the cursor at the next line.
Now the value of i and j is increased to 3.
Iteration 4:
The first print statement prints four stars at the fourth row and the second println statement prints the spaces and throws the cursor at the next line.
Now the value of i and j is increased to 4.
The execution of the program will terminate when the value of i will be equal to the number of rows.
Star Pattern
1. Right Triangle Star Pattern
Output:
2. Left Triangle Star Pattern
Output:
3. Pyramid Star Pattern
Output:
4. Diamond Shape Pattern
Output:
5. Downward Triangle Star Pattern
Output:
6. Mirrored Right Triangle Star Pattern
Output:
7. Reverse Pyramid Star Pattern
Output:
8. Right Down Mirror Star Pattern
Output:
9. Right Pascal's Triangle
Output:
10. Left Pascal's Triangle
Output:
Java Program To Print Number Patterns Programs Free
11. Sandglass Star Pattern
Output:
12. Alphabet Star Pattern
Output:
13. Triangle Star Pattern
Output:
Java Program To Print Number Patterns Programs Pdf
14. Down Triangle Pattern
Output:
15. Diamond Star Pattern
Output:
Number Pattern
1. Pattern-1
Output:
2. Pattern-2
Output:
3. Pattern-3
Output:
4. Pattern-4
Output:
5. Pattern-5
Output:
6. Pattern-6
Output:
7. Pattern-7
Output:
8. Pattern-8
Output:
9. Pattern-9
Output:
10. Pattern-10
Output:
11. Pattern-11
Output:
12. Pattern-12
Output:
13. Pattern-13
Output:
14. Pattern-14
Output:
15. Pattern-15
Output:
16. Pattern-16
Output:
17. Pattern-17
Output:
18. Pattern-18
Output:
19. Pattern-19
Output:
20. Pattern-20
Output:
21. Pattern-21
Output:
Character Pattern
1. Right Triangle Alphabetic Pattern
Output:
2. Repeating Alphabet Pattern
Output:
3. K-shape Alphabet Pattern
Output:
4. Triangle Character Pattern
Output:
5. Diamond Character Pattern
Output:
Square frame pattern is designed in accordance to the number of rows user has entered.
In this java program, the user is asked to enter number of rows. It displays a hallow square based on number of rows.
Algorithm:
Step 1: Start.
Step 2: Read number of rows to be printed. Store it in some variable, say n.
Step 3: To iterate through rows, run an outer loop from 1 to n. For that define a for loop.
Step 4: To iterate through columns, run an inner loop from 1 to n. Define a for loop.
Step 5: Inside inner loop print star for the first and last rows and first and last column. Otherwise enter space.
Step 6: Stop.
Source code:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | publicclassSquare{ publicstaticvoidmain(String[]args){ System.out.println('n n *** www.programmingposts.com *** n n'); System.out.println('<< JAVA Program to print hallow Square pattern >> n n'); System.out.println('Enter number of rows:'); for(inti=1;i<=rows;i++) for(intj=1;j<=rows;j++) if(i1||irows||j1||jrows) System.out.print('* '); else System.out.print(' '); } System.out.println(); |
Explanation:
for(int i=1;i<=rows;i++) – To iterate through rows run an outer loop from 1 to rows.
for(int j=1;j<=rows;j++) – To iterate through columns, run an inner loop from 1 to rows.
if(i1 || irows || j1 || jrows) – Inside inner loop print star for first and last row or for first and last column.
else print space.
Output:
C:UserssantoDesktopnew>javac Square.java
Print Number Plates
C:UserssantoDesktopnew>java Square
*** www.programmingposts.com ***
<< JAVA Program to print hallow square pattern >>
Enter number of rows:
6
* * * * * *
* *
* *
* *
* *
* * * * * *