[Go to site: main page, start]

0% found this document useful (0 votes)
12 views2 pages

Java Pattern Printing Examples Guide

This guide provides step-by-step instructions for printing various patterns in Java using nested loops. It includes examples for square, right triangle, inverted triangle, pyramid, and diamond patterns, along with corresponding code snippets. Each pattern demonstrates how to control rows and columns effectively.

Uploaded by

skimranmbdhfm
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)
12 views2 pages

Java Pattern Printing Examples Guide

This guide provides step-by-step instructions for printing various patterns in Java using nested loops. It includes examples for square, right triangle, inverted triangle, pyramid, and diamond patterns, along with corresponding code snippets. Each pattern demonstrates how to control rows and columns effectively.

Uploaded by

skimranmbdhfm
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 Printing Guide

This guide will help you understand Java pattern printing step by step. Patterns mostly use nested
loops: - Outer loop → controls rows - Inner loop → controls columns (stars, spaces, numbers, etc.)

1. Square Pattern
Pattern:
*****
*****
*****
Code:
for(int i=1; i<=3; i++) {
for(int j=1; j<=5; j++) {
[Link]("*");
}
[Link]();
}

2. Right Triangle
Pattern:
*
**
***
****
Code:
for(int i=1; i<=4; i++) {
for(int j=1; j<=i; j++) {
[Link]("*");
}
[Link]();
}

3. Inverted Triangle
Pattern:
****
***
**
*
Code:
for(int i=4; i>=1; i--) {
for(int j=1; j<=i; j++) {
[Link]("*");
}
[Link]();
}

4. Pyramid
Pattern:
*
***
*****
Code:
for(int i=1; i<=3; i++) {
for(int j=1; j<=3-i; j++) {
[Link](" ");
}
for(int k=1; k<=2*i-1; k++) {
[Link]("*");
}
[Link]();
}

5. Diamond
Pattern:
*
***
*****
***
*
Code:
int n = 3;
// upper half
for(int i=1; i<=n; i++) {
for(int j=1; j<=n-i; j++) {
[Link](" ");
}
for(int k=1; k<=2*i-1; k++) {
[Link]("*");
}
[Link]();
}
// lower half
for(int i=n-1; i>=1; i--) {
for(int j=1; j<=n-i; j++) {
[Link](" ");
}
for(int k=1; k<=2*i-1; k++) {
[Link]("*");
}
[Link]();
}

You might also like