Star Pattern Programs in Java

Star Pattern Programs in Java

9 mins read154 Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on Apr 18, 2024 14:13 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.

2023_10_Copy-of-What-is-14.jpg

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.

Why Are Star Pattern Programs Important?

  • Understanding Control Structures: Assists in learning and mastering loops and control structures by providing practical, hands-on experience.
  • Problem-Solving Skills: Bolsters logical thinking and problem-solving abilities as it requires innovative thinking to form specific patterns.
  • Attention to Detail: Enhances focus on minute details, essential for precise pattern creation and efficient coding.
  • Preparation for Interviews: Often used in technical interviews, aiding invaluable preparation and practice for real-world coding assessments.

Basic Approach to Solve a Star Pattern Program Using an Example

A step-by-step guide using a simple example: printing a right-angled triangle star pattern.

Steps to Solve

1. Understand the Pattern

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.

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.


 
for(int i=1; i<=5; i++) {
}
Copy code

3. Nested Loop for Printing Stars

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.


 
for(int j=1; j<=i; j++) {
System.out.print("*");
}
Copy code

4. Move to the Next Line After Printing Stars in a Row

After printing stars in a row, print a newline character to move to the next row.


 
System.out.println();
Copy code

Complete Code


 
public class Main {
public static void main(String[] args) {
// Outer loop for number of rows
for (int i = 1; i <= 5; i++) {
// Inner loop for number of stars in each row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Move to the next line after printing stars in each row
System.out.println();
}
}
}
Copy code

This basic approach can be adapted to solve various other star pattern programs in Java.

Top 10 Star Pattern Programs in Java

  1. Square Star Pattern
  2. Inverted Pyramid Star Pattern
  3. Pyramid Star Pattern
  4. Diamond Star Pattern
  5. Hollow Square Star Pattern
  6. Butterfly Pattern
  7. Downward Triangle Star Pattern
  8. Hollow Diamond Star Pattern
  9. Cross Star Pattern
  10. Hollow Pyramid Star Pattern

Let’s understand each of these one by one in detail :

1. Square Star Pattern: This pattern forms a square with stars.


 
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Copy code

2. Inverted Pyramid Star Pattern: This pattern forms an inverted pyramid with stars.

 


 
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
// Print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print asterisks
for (int j = i; j < 5; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Copy code

3. Pyramid Star Pattern: This pattern forms a pyramid with stars.

 


 
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
// Print spaces
for (int j = 5; j > i; j--) {
System.out.print(" ");
}
// Print asterisks
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Copy code

4. Diamond Star Pattern: This pattern prints a diamond shape with stars.

 


 
public class Main {
public static void main(String[] args) {
int n = 5;
// Upper half of the diamond
for (int i = 0; i < n; i++) {
// Print spaces
for (int j = n - 1; j > i; j--) {
System.out.print(" ");
}
// Print asterisks
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
// Lower half of the diamond
for (int i = 1; i < n; i++) {
// Print spaces
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();
}
}
}
Copy code
 

5. Hollow Square Star Pattern: This pattern prints a square but leaves the middle portion hollow.

 


 
public class Main {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Check if it's a border position
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Copy code

6. Butterfly Pattern: This pattern looks like a butterfly, with two symmetrical sides filled with stars.

 


 
public class Main {
public static void main(String[] args) {
int n = 5;
// Upper half of the diamond
for (int i = 1; i <= n; i++) {
// Print left half of the row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Print spaces in the middle
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Print right half of the row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower half of the diamond
for (int i = n; i >= 1; i--) {
// Print left half of the row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Print spaces in the middle
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Print right half of the row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Copy code
Pascal’s Triangle – Definition, Properties, Applications and More
Pascal’s Triangle – Definition, Properties, Applications and More
Pascal’s Triangle, named after the French mathematician Blaise Pascal, has roots that stretch much further back in history. The discovery of Pascal’s Triangle wasn’t a single event but rather a...read more
Floyd’s Triangle – Definition, Properties and More
Floyd’s Triangle – Definition, Properties and More
Floyd’s Triangle is a right-angled triangular array of natural numbers named after the American computer scientist Robert W. Floyd. Floyd's Triangle is a simple geometric arrangement of numbers. Let us...read more
Array Programs in Java | Beginner to Expert Level
Array Programs in Java | Beginner to Expert Level
Array programs in Java traverse from basic single-dimensional arrays to complex multi-dimensional arrays and dynamic arrays using ArrayList. From initializing and accessing array elements, to advanced operations like sorting and...read more

7. Downward Triangle Star Pattern: This pattern forms a downward-facing triangle with stars.

 


 
public class Main {
public static void main(String[] args) {
// Loop to print rows
for (int i = 5; i >= 1; i--) {
// Loop to print asterisks in each row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Move to the next line after each row
System.out.println();
}
}
}
Copy code

8. Hollow Diamond Star Pattern: This pattern prints a diamond shape with stars, leaving the middle portion hollow.

 


 
public class Main {
public static void main(String[] args) {
int n = 5;
// Upper half of the diamond
for (int i = 0; i < n; i++) {
// Print left side of the upper half
for (int j = i; j < n; j++) {
System.out.print("*");
}
// Print spaces in the middle
for (int k = 0; k < 2 * i; k++) {
System.out.print(" ");
}
// Print right side of the upper half
for (int j = i; j < n; j++) {
System.out.print("*");
}
System.out.println();
}
// 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();
}
}
}
Copy code

9. Cross Star Pattern: This pattern prints a cross shape with stars.

 


 
public class Main {
public static void main(String[] args) {
int n = 7; // Must be odd
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Check if the current position is in the center row or center column
if (i == n / 2 || j == n / 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
// Move to the next line after each row
System.out.println();
}
}
}
Copy code

10. Hollow Pyramid Star Pattern: This pattern forms a pyramid with stars, leaving the middle portion hollow.

 


 
public class Main {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
// Print leading spaces
for (int j = n - 1; j > i; j--) {
System.out.print(" ");
}
// Print the first and last rows
if (i == 0 || i == n - 1) {
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
} else {
// Print the first asterisk
System.out.print("*");
// Print spaces inside the pyramid
for (int k = 1; k < i; k++) {
System.out.print(" ");
}
// Print the second asterisk
System.out.print(" *");
}
// Move to the next line
System.out.println();
}
}
}
Copy code

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

What are star pattern programs in Java?

Star pattern programs are Java programs designed to print a series of asterisk (*) characters on the console in various shapes or designs, like triangles, squares, or diamonds. These patterns are commonly used to practice nested loops and understand the concept of pattern formation in programming.

How do you create a simple triangle star pattern in Java?

To create a triangle star pattern, you typically use nested loops: an outer loop to handle the number of lines and an inner loop to print the appropriate number of stars per line. Each subsequent line increases the number of stars, creating a triangular shape.

Can you modify star patterns to use other characters besides the asterisk?

Yes, star patterns in Java can be modified to use any character. By replacing the asterisk (*) in the code with another character, such as #, $, or even a number or letter, you can create patterns with different visual appearances.

What are some common variations of star patterns in Java?

Common variations include the right-angled triangle, inverted triangle, pyramid, inverted pyramid, diamond shape, and hollow patterns. Each variation involves tweaking the looping logic to change the orientation or fill of the pattern.

Why are star pattern programs important for Java learners?

Star pattern programs are important for Java learners as they provide a practical way to understand the use of loops (for, while, do-while) and conditional statements. They help in developing logical thinking and problem-solving skills, which are essential for programming. Additionally, they are a common exercise in academic courses and coding interviews to test a candidate's grasp of basic programming constructs.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio