![]() |
VOOZH | about |
In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts (especially for loop) and problem-solving skills in Java. If you are looking for a place to get all the Java pattern programs with solutions, stop your search here.
Here, we have compiled a top pattern exercises on Java.
Prerequisite: Remember that to learn pattern programs, you must know Java Loops (for, while, do-while) and basic syntax.
Here, you will find the top 25 Java pattern programs with their proper code and explanation.
All Pattern Programs in Java are mentioned below:
This program prints a square where the border is filled with stars (*), and the inside is hollow (filled with spaces).
****** * * * * * * * * ******
Prints a right-angled triangle with numbers in increasing row order, aligned to the right.
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6
Prints a pyramid where each row contains numbers from 1 to the row number.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6
This is a reverse pyramid where each row starts from 1 and ends at the row count, in decreasing number of elements.
1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Prints a pyramid where numbers increase continuously from top to bottom.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Prints a triangle where each number alternates between 1 and 0 based on the position.
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1
Prints a triangle with mirrored numbers forming a palindrome on each row.
1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6
Prints a rhombus (tilted square) made of stars, shifted by spaces.
****** ****** ****** ****** ****** ******
Prints a diamond shape made of stars.
* *** ***** ******* ********* *********** ********* ******* ***** *** *
Prints a butterfly-shaped pattern using stars (*).
* * ** ** *** *** **** **** ***** ***** ************ ************ ***** ***** **** **** *** *** ** ** * *
This program prints a filled square of stars (*) with n+1 rows and columns.
****** ****** ****** ****** ****** ******
This creates a right-angled triangle aligned to the left using stars.
* ** *** **** ***** ******
This prints a right-aligned triangle but in reverse top to bottom.
****** ***** **** *** ** *
This pattern aligns a triangle to the right by adding spaces before stars.
* ** *** **** ***** ******
This prints a left pyramid in reverse, starting from full-width and decreasing.
****** ***** **** *** ** *
This prints an equilateral triangle where stars are spaced apart.
* * * * * * * * * * * * * * * * * * * * *
A reverse right-aligned triangle with ascending numbers from i to n.
1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6
This creates a mirrored triangle with numbers, forming a diamond-like shape.
1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 5 6 4 5 6 3 4 5 6 2 3 4 5 6 1 2 3 4 5 6
This prints a hollow equilateral triangle using stars with only the boundary.
* * * * * * * * * ***********
Prints an inverted hollow triangle with stars only at boundaries and top row.
*********** * * * * * * * * *
Prints a symmetrical hollow diamond made of stars, with spaces in the middle and stars only at the borders.
* * * * * * * * * * * * * * * * * * * *
Displays a hollow hourglass using stars, where only boundary stars are visible in a symmetric hourglass shape.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Generates Pascal’s Triangle with proper alignment, showing binomial coefficients for each row.
1 1 1 1 2 1 1 3 3 1
Forms a right-angled triangle pointing upward and downward, creating a right Pascal’s triangle shape.
* * * * * * * * * * * * * * * *
Prints an inverted and then upright right half pyramid (like K shaped), forming a symmetrical star pattern.
****** ***** **** *** ** * ** *** **** ***** ******