Javatpoint Logo
Javatpoint Logo

How to Print Pattern in Java

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:

  • 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.

How to Print Pattern in Java

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:

For i=0, 0<4 (true)
For j=0, j<=0 (true)

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:

For i=1, 1<4 (true)
For j=1, 1<=1 (true)

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:

For i=2, 2<4 (true)
For j=2, 2<=2 (true)

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:

For i=3, 3<4 (true)
For j=3, 3<=3 (true)

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.

For i=4, 4<4 (false)

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:

How to Print Pattern in Java

2. Left Triangle Star Pattern

Output:

How to Print Pattern in Java

3. Pyramid Star Pattern

Output:

How to Print Pattern in Java

4. Diamond Shape Pattern

Output:

How to Print Pattern in Java

5. Downward Triangle Star Pattern

Output:

How to Print Pattern in Java

6. Mirrored Right Triangle Star Pattern

Output:

How to Print Pattern in Java

7. Reverse Pyramid Star Pattern

Output:

How to Print Pattern in Java

8. Right Down Mirror Star Pattern

Output:

How to Print Pattern in Java

9. Right Pascal's Triangle

Output:

How to Print Pattern in Java

10. Left Pascal's Triangle

Output:

How to Print Pattern in Java

11. Sandglass Star Pattern

Output:

How to Print Pattern in Java

12. Alphabet Star Pattern

Output:

How to Print Pattern in Java

13. Triangle Star Pattern

Output:

How to Print Pattern in Java

14. Down Triangle Pattern

Output:

How to Print Pattern in Java

15. Diamond Star Pattern

Output:

How to Print Pattern in Java

Number Pattern

1. Pattern-1

Output:

How to Print Pattern in Java

2. Pattern-2

Output:

How to Print Pattern in Java

3. Pattern-3

Output:

How to Print Pattern in Java

4. Pattern-4

Output:

How to Print Pattern in Java

5. Pattern-5

Output:

How to Print Pattern in Java

6. Pattern-6

Output:

How to Print Pattern in Java

7. Pattern-7

Output:

How to Print Pattern in Java

8. Pattern-8

Output:

How to Print Pattern in Java

9. Pattern-9

Output:

How to Print Pattern in Java

10. Pattern-10

Output:

How to Print Pattern in Java

11. Pattern-11

Output:

How to Print Pattern in Java

12. Pattern-12

Output:

How to Print Pattern in Java

13. Pattern-13

Output:

How to Print Pattern in Java

14. Pattern-14

Output:

How to Print Pattern in Java

15. Pattern-15

Output:

How to Print Pattern in Java

16. Pattern-16

Output:

How to Print Pattern in Java

17. Pattern-17

Output:

How to Print Pattern in Java

18. Pattern-18

Output:

How to Print Pattern in Java

19. Pattern-19

Output:

How to Print Pattern in Java

20. Pattern-20

Output:

How to Print Pattern in Java

21. Pattern-21

Output:

How to Print Pattern in Java

Character Pattern

1. Right Triangle Alphabetic Pattern

Output:

How to Print Pattern in Java

2. Repeating Alphabet Pattern

Output:

How to Print Pattern in Java

3. K-shape Alphabet Pattern

Output:

How to Print Pattern in Java

4. Triangle Character Pattern

Output:

How to Print Pattern in Java

5. Diamond Character Pattern

Output:

How to Print Pattern in Java
Next TopicJava Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA