0% found this document useful (0 votes)
0 views

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)
0 views

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
You are on page 1/ 5

Java Pattern Programs

1. Triangle Pattern Using String.repeat()

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

int n = 5;
for (int i = 1; i <= n; i++) {
System.out.println(" ".repeat(n - i) + "*".repeat(i * 2 - 1));
}

Output:

***

*****

*******

*********

2. Center-Aligned Number Triangle Using String.format()

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

int n = 5;
for (int i = 1; i <= n; i++) {
System.out.println(String.format("%" + (n - i) + "s", "").replace(" ", " ") + "*".repeat(i * 2
- 1));
}

Output:

***

*****

*******

*********
3. Diamond Pattern Using Math.abs()

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

int n = 5;
for (int i = -n + 1; i < n; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat(2 * (n - Math.abs(i)) - 1));
}

Output:

***

*****

*******

*********

*******

*****

***

4. Powers of 2 Pattern Using Math.pow()

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++) {
System.out.println((int)Math.pow(2, i));
}

Output:

2
4

16

5. Comma-Separated Stars Using String.join()

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++) {
System.out.println(String.join(",", "*".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++) {
sb.append("*");
System.out.println(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++) {
System.out.println("*".repeat(n));
}

Output:

*****

*****

*****

*****

*****

Rectangle Pattern (5x3)


Program:

int rows = 3, columns = 5;


for (int i = 0; i < rows; i++) {
System.out.println("*".repeat(columns));
}

Output:

*****
*****

*****

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy