Pattern Java
Pattern Java
Esha Gupta
Associat e Senior Execut ive
Updated on Oct 13, 2024 20:57 IST
Pattern programs in Java are a type of problem that uses nested loops to
produce different patterns of numbers, stars (*), or other characters. In this
blog, we will dive deeper, specifically into star pattern programs in Java.
Star pattern programs in Java are popular for practising nested loop control
structures. They involve using loops to print out a particular pattern of stars (*) on
the screen.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
mastering loops and control structures by providing practical, hands-
on experience.
Steps to Solve
Analyze the pattern to understand the relation between the row numbers and the
number of stars in each row. In this example, the number of stars in each row equals
the row number.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
2. Initialize the Loop for Rows
Use a loop to iterate through the rows. The number of iterations should equal the
number of rows in the pattern.
Copy code
Inside the loop for rows, use another loop to print stars in each row. The number of
stars is equal to the current row number.
Copy code
After printing stars in a row, print a newline character to move to the next row.
Copy code
System.out.println();
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Complete Code
Copy code
This basic approach can be adapted to solve various other star pattern programs in
Java.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Downward Triangle Star Pattern
Hollow Diamond Star Pattern
Copy code
2. Inverted Pyramid Star Pattern: This pattern forms an inverted pyramid with
stars.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
4. Diamond Star Pattern: This pattern prints a diamond shape with stars.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print asterisks
for (int k = n - 1; k > = i; k--) {
System.out.print("* ");
}
System.out.println();
}
}
}
5. Hollow Square Star Pattern: This pattern prints a square but leaves the
middle portion hollow.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
6. Butterf ly Pattern: This pattern looks like a butterfly, with two symmetrical
sides filled with stars.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
System.out.print(" ");
}
// Print right half of the row
for (int j = 1; j < = i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
8. Hollow Diamond Star Pattern: This pattern prints a diamond shape with
stars, leaving the middle portion hollow.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
// Lower half of the diamond
for (int i = 1; i < n; i++) {
// Print left side of the lower half
for (int j = 0; j < = i; j++) {
System.out.print("*");
}
// Print spaces in the middle
for (int k = 2 * (n - i - 1); k > 0; k--) {
System.out.print(" ");
}
// Print right side of the lower half
for (int j = 0; j < = i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
9. Cross Star Pattern: This pattern prints a cross shape with stars.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
10. Hollow Pyramid Star Pattern: This pattern forms a pyramid with stars,
leaving the middle portion hollow.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.
}
}
Conclusion
Thus, star pattern programs in Java are an essential part of foundational
programming learning and practice. They help understand the use and manipulation
of loops and control statements and enhance logical thinking and problem-solving
skills.
FAQs
Can you modif y star patterns to use other characters besides the
asterisk?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Oct-20 24.