0% found this document useful (0 votes)
133 views5 pages

Java Pattern Programs

The document provides Java code examples for creating various patterns, including triangles, diamonds, and squares, using different string manipulation techniques. Each example includes an explanation, the code, and the expected output. Key methods used include String.repeat(), String.format(), Math.abs(), Math.pow(), String.join(), and StringBuilder.

Uploaded by

akbaralimay7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views5 pages

Java Pattern Programs

The document provides Java code examples for creating various patterns, including triangles, diamonds, and squares, using different string manipulation techniques. Each example includes an explanation, the code, and the expected output. Key methods used include String.repeat(), String.format(), Math.abs(), Math.pow(), String.join(), and StringBuilder.

Uploaded by

akbaralimay7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Pattern Programs

1. Triangle Pattern Using [Link]()

Explanation: This code creates a right-aligned triangle pattern with stars using Java's [Link]() function.
Program:

int n = 5;
for (int i = 1; i <= n; i++) {
[Link](" ".repeat(n - i) + "*".repeat(i * 2 - 1));
}

Output:

***

*****

*******

*********

2. Center-Aligned Number Triangle Using [Link]()

Explanation: This example centers each row of asterisks in a triangle shape using [Link]() to add padding.
Program:

int n = 5;
for (int i = 1; i <= n; i++) {
[Link]([Link]("%" + (n - i) + "s", "").replace(" ", " ") + "*".repeat(i * 2
- 1));
}

Output:

***

*****

*******

*********
3. Diamond Pattern Using [Link]()

Explanation: This code generates a diamond pattern with a center alignment using spaces and [Link]() for symmetry.
Program:

int n = 5;
for (int i = -n + 1; i < n; i++) {
[Link](" ".repeat([Link](i)) + "*".repeat(2 * (n - [Link](i)) - 1));
}

Output:

***

*****

*******

*********

*******

*****

***

4. Powers of 2 Pattern Using [Link]()

Explanation: This code generates a sequence of powers of 2, showing values from 2^0 up to 2^(n-1).
Program:

int n = 5;
for (int i = 0; i < n; i++) {
[Link]((int)[Link](2, i));
}

Output:

2
4

16

5. Comma-Separated Stars Using [Link]()

Explanation: This program separates stars with commas and increases the count on each row, creating a visually

distinct pattern.
Program:

int n = 5;
for (int i = 1; i <= n; i++) {
[Link]([Link](",", "*".repeat(i).split("")));
}

Output:

*,*

*,*,*

*,*,*,*

*,*,*,*,*

6. Right-Angle Triangle Using StringBuilder

Explanation: This code builds a right-angle triangle by adding an extra * on each new row using a StringBuilder for

efficient string handling.


Program:

int n = 5;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
[Link]("*");
[Link](sb);
}

Output:
*

**

***

****

*****

Additional Repeated Pattern Examples

Here are a few examples of repeated patterns such as squares or rectangular patterns made using nested loops.

Square Pattern
Program:

int n = 5;
for (int i = 0; i < n; i++) {
[Link]("*".repeat(n));
}

Output:

*****

*****

*****

*****

*****

Rectangle Pattern (5x3)


Program:

int rows = 3, columns = 5;


for (int i = 0; i < rows; i++) {
[Link]("*".repeat(columns));
}

Output:

*****
*****

*****

You might also like