Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Pattern Java

Uploaded by

Aswath Maddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Pattern Java

Uploaded by

Aswath Maddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Star Pattern Programs in 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.

Also, read Number Pattern Programs in Java

Explore Pattern Programs in C

Why Are Star Pattern Programs Important?


Understanding Control Structures: Assists in learning and

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.

Problem-Solving Skills: Bolsters logical thinking and problem-


solving abilities as it requires innovative thinking to f orm specif ic
patterns.

Attention to Detail: Enhances f ocus on minute details, essential f or


precise pattern creation and ef f icient coding.
Preparation f or Interviews: Of ten used in technical interviews,
aiding invaluable preparation and practice f or 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.

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

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


}

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.

Copy code

for(int j=1; j< =i; j++) {


System.out.print("*");
}

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.

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

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();
}
}
}

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

Top 10 Star Pattern Programs in Java


Square Star Pattern
Inverted Pyramid Star Pattern
Pyramid Star Pattern

Diamond Star Pattern


Hollow Square Star Pattern
Butterf ly Pattern

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

Cross Star Pattern


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.

Copy code

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();
}
}
}

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

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();
}
}
}

3. Pyramid Star Pattern: This pattern forms a 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

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();
}
}
}

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

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++) {

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

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();
}
}
}

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

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++) {

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();
}
}
}

Pascal’s Triangle – Def init ion, Propert ies, Applicat ions…


and More
Pascal’s Triangle, named after the French mathematician Blaise Pascal, has
ro o ts that stretch much further back in histo ry. The disco very o f Pascal’s Triangle wasn’t a single event but
rather a...re ad m o re

Floyd’s Triangle – Def init ion, Propert ies and More


Flo yd’s Triangle is a right-angled triangular array o f natural numbers named after
the American co mputer scientist Ro bert W. Flo yd. Flo yd's Triangle is a simple
geo metric arrangement o f numbers. Let us...re ad m o re

Array Programs in Java | Beginner t o Expert Level


Array pro grams in Java traverse fro m basic single-dimensio nal arrays to
co mplex multi-dimensio nal arrays and dynamic arrays using ArrayList. Fro m
initializing and accessing array elements, to advanced o peratio ns like so rting
and...re ad m o re

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


triangle 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

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();
}
}
}

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

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();
}

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

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();
}
}
}

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

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();
}
}

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

What are star pattern programs in Java?

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

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

What are some common variations of star patterns in Java?

Why are star pattern programs important f or Java learners?

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.

You might also like