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

Java Pattern

The document outlines various basic pattern formulas for printing shapes using nested loops in programming. It includes examples for left-aligned triangles, number triangles, alphabet triangles, inverted triangles, pyramids, inverted pyramids, and diamonds, specifying the outer and inner loop structures. Each pattern is described with the corresponding code snippets for generating the desired output.

Uploaded by

Sanjay Sonule
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)
2 views2 pages

Java Pattern

The document outlines various basic pattern formulas for printing shapes using nested loops in programming. It includes examples for left-aligned triangles, number triangles, alphabet triangles, inverted triangles, pyramids, inverted pyramids, and diamonds, specifying the outer and inner loop structures. Each pattern is described with the corresponding code snippets for generating the desired output.

Uploaded by

Sanjay Sonule
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/ 2

🔁 Basic Pattern Formula:

for (int i = 1; i <= rows; i++) { // How many rows

for (int j = 1; j <= ?; j++) { // What to print

System.out.print( ? ); // Star? Number? Space?


}
System.out.println(); // Go to next line
}

Pattern Name Outer Loop (Rows) What to Print Formula for Inner Loop
Print stars i times in each row:

*
** for (int j = 1; j <= i; j++)
Left-Aligned Triangle for (int i = 1; i <= rows; i++) *** { System.out.print("*"); }
Print numbers from 1 to i:

1
12 for (int j = 1; j <= i; j++)
Number Triangle for (int i = 1; i <= rows; i++) 123 { System.out.print(j); }
Print alphabets from A to i:

A
AB for (int j = 1; j <= i; j++)
Alphabet Triangle for (int i = 1; i <= rows; i++) ABC { System.out.print((char)('A' + j - 1)); }
Print stars rows - i + 1 times in
each row:

*****
**** for (int j = 1; j <= rows - i + 1; j++)
Inverted Triangle for (int i = 1; i <= rows; i++) *** { System.out.print("*"); }
Print spaces, then stars: for (int j = 1; j <= rows - i; j++)
{ System.out.print(" "); }
*
*** for (int j = 1; j <= 2 * i - 1; j++)
Pyramid (Centered Stars) for (int i = 1; i <= rows; i++) ***** { System.out.print("*"); }
Print spaces, then stars: for (int j = 1; j <= rows - i; j++)
{ System.out.print(" "); }
*********
******* for (int j = 1; j <= 2 * i - 1; j++)
Inverted Pyramid for (int i = rows; i >= 1; i--) ***** { System.out.print("*"); }
Print spaces, then stars: for (int j = 1; j <= rows - i; j++)
{ System.out.print(" "); }
*
*** for (int j = 1; j <= 2 * i - 1; j++)
Diamond (Top Half) for (int i = 1; i <= rows; i++) ***** { System.out.print("*"); }
Print spaces, then stars: for (int j = 1; j <= rows - i; j++)
{ System.out.print(" "); }
*****
*** for (int j = 1; j <= 2 * i - 1; j++)
Diamond (Bottom Half) for (int i = rows - 1; i >= 1; i--) * { System.out.print("*"); }

You might also like