0% found this document useful (0 votes)
7 views19 pages

Java Patterns: Rectangle & Rhombus

The document contains Java code for 16 different patterns, each demonstrating various ways to print shapes using asterisks or numbers. Patterns include solid and hollow rectangles, pyramids, inverted shapes, and butterflies, providing a comprehensive guide for creating visual patterns in console applications. Each pattern prompts the user to enter the number of rows and uses nested loops to generate the desired output.

Uploaded by

rajkumarshaw21r
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)
7 views19 pages

Java Patterns: Rectangle & Rhombus

The document contains Java code for 16 different patterns, each demonstrating various ways to print shapes using asterisks or numbers. Patterns include solid and hollow rectangles, pyramids, inverted shapes, and butterflies, providing a comprehensive guide for creating visual patterns in console applications. Each pattern prompts the user to enter the number of rows and uses nested loops to generate the desired output.

Uploaded by

rajkumarshaw21r
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

PATTERN 1:

/* Pattern 1: Solid Rectangle */

package Patterns;

import [Link].* ;

class Pattern1 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();
for(int i = 1; i<=n; i++){
for(int j=1;j<=n;j++){
[Link]("*");
}
[Link]();
}
[Link]();
}
}
PATTERN 2:

/* Pattern 2: Hollow Rectangle */


package Patterns;
import [Link].* ;

public class Pattern2 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();

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


for(int j=1;j<=n;j++){
if(i==1 || i==n || j==1 || j==n){
[Link]("*");
}
else{
[Link](" ");
}
}
[Link]();
}
[Link]();
}

}
PATTERN 3:

/* Pattern 3: Half Pyramid */


package Patterns;
import [Link].* ;

class Pattern3 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();

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


for(int j=1;j<=i;j++)
[Link]("*");

[Link]();
}
[Link]();

}
PATTERN 4:

/* Pattern 4: Inverted Half Pyramid


*/
package Patterns;
import [Link].* ;

public class Pattern4 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++)
[Link]("*");

[Link]();
}
[Link]();
}
}
PATTERN 5:

/* Pattern 5: Inverted & Rotated Half


Pyramid */

package Patterns;
import [Link].* ;

public class Pattern5 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i);j++)
[Link](" ");
for(int j=1;j<=i;j++)
[Link]("*");

[Link]();
}
[Link]();
}

}
PATTERN 6:

/* Pattern 6: Half Pyramid With


Numbers */
package Patterns;
import [Link].* ;

public class Pattern6 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++)
[Link](j);
[Link]();
}
[Link]();
}
}
PATTERN 7:

/* Pattern 7: Inverted Half Pyramid with Numbers */

package Patterns;
import [Link].* ;

class Pattern7{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();

for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++)
[Link](j);
[Link]();
}
[Link]();
}
}
PATTERN 8:

/* Pattern 8: Floyd's Triangle */

package Patterns;
import [Link].* ;

class Pattern8{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();
int k = 0;

for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++)
[Link](++k + " ");
[Link]();
}
[Link]();
}
}
PATTERN 9:

/* Pattern 9: 0 - 1 Triangle */

package Patterns;
import [Link].* ;

class Pattern9{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();

for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
if((i+j)%2==0)
[Link]("1 ");
else
[Link]("0 ");
}
[Link]();
}
[Link]();
}
}
PATTERN 10:

/* Pattern 10: Solid Rhombus */

package Patterns;
import [Link].* ;

public class Pattern10 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i);j++)
[Link](" ");
for(int j=1;j<=n;j++)
[Link]("*");
[Link]();
}
[Link]();
}
}
PATTERN 11:

/* Pattern 11: Number Pyramid */

package Patterns;
import [Link].* ;

class Pattern11 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i);j++)
[Link](" ");
for(int j=1;j<=i;j++)
[Link](i + " ");
[Link]();
}
[Link]();
}

}
PATTERN 12:

/* Pattern 12: Palindromic Number


Pyramid */

package Patterns;
import [Link].* ;

public class Pattern12 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i);j++)
[Link](" ");
for(int j=i;j>=1;j--)
[Link](j);
for(int j=2;j<=i;j++)
[Link](j);
[Link]();
}
[Link]();

}
PATTERN 13:

/* Pattern 13: Solid Butterfly */

package Patterns;
import [Link].* ;

public class Pattern13 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

//upper part
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++)
[Link]("*");
for(int j=1;j<=2*(n-i);j++)
[Link](" ");
for(int j=1;j<=i;j++)
[Link]("*");
[Link]();
}

//lower part
for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++)
[Link]("*");
for(int j=1;j<=2*(n-i);j++)
[Link](" ");
for(int j=1;j<=i;j++)
[Link]("*");
[Link]();
}
[Link]();
}
}
PATTERN 14:

/* Pattern 14: Diamond */


package Patterns;
import [Link].* ;

public class Pattern14 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

//First Part
for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i);j++)
[Link](" ");
for(int j=1;j<=(2*i-1);j++)
[Link]("*");
[Link]();
}

//Second Part
for(int i=n;i>=1;i--){
for(int j=1;j<=(n-i);j++)
[Link](" ");
for(int j=1;j<=(2*i-1);j++)
[Link]("*");
[Link]();
}
[Link]();
}
}
PATTERN 15:

/* Pattern 15: Hollow Butterfly */

package Patterns;
import [Link].* ;

public class Pattern15 {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();

//upper part
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++)
if(j==1||i==j)
[Link]("*");
else
[Link](" ");
for(int j=1;j<=2*(n-i);j++) //printing spaces
[Link](" ");

for(int j=1;j<=i;j++)
if(j==1||i==j)
[Link]("*");
else
[Link](" ");
[Link](); //new line
}
//lower part
for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++)
if(j==1||i==j)
[Link]("*");
else
[Link](" ");

for(int j=1;j<=2*(n-i);j++) //printing spaces


[Link](" ");

for(int j=1;j<=i;j++)
if(j==1||i==j)
[Link]("*");
else
[Link](" ");

[Link](); //new line


}
[Link]();
}

}
PATTERN 16:

/* Pattern 16: Hollow Rhombus */

package Patterns;
import [Link].* ;
public class Pattern16 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number of Rows: ");
int n = [Link]();
for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i);j++)
[Link](" ");

for(int j=1;j<=n;j++)
if(i==1||i==n||j==1||j==n)
[Link]("*");
else
[Link](" ");

[Link]();
}
[Link]();
}

Common questions

Powered by AI

Both Solid Rhombus (Pattern 10) and Diamond (Pattern 14) employ spaces to achieve their distinct geometric shapes. In the Solid Rhombus, spaces are printed in a decreasing count before each row of asterisks, resulting in the asterisks forming a rhombus shape. For the Diamond, the upper half increases stars between decreasing spaces, whereas the lower half is a mirror image, creating a symmetric diamond shape across a central axis. This careful placement of initial spaces effectively centres the stars, defining the geometry visualized in each pattern .

The implementation of a solid rhombus (Pattern 10) involves two nested loops: the outer loop manages the number of rows, ensuring each row begins with a certain number of spaces followed by n asterisks. The inner loop for spaces decreases as the row number increases. Conversely, the solid rectangle (Pattern 1) uses two straightforward nested loops without space padding; both loops iterate completely over 'n' to print n asterisks per row. This makes the rhombus require a manipulation of spaces to achieve its shape, whereas the rectangle does not .

In the 0-1 triangle pattern (Pattern 9), the logic '(i + j) % 2 == 0' ensures alternating numbers in the output. The use of this condition alternates the print of '1' and '0'. For each row 'i', it checks if the sum 'i+j' is even or odd, printing '1' for even sums and '0' otherwise. This results in an alternating binary pattern within the triangle structure, creating visual diversity based on row and column indices .

The differentiation between a hollow rectangle and a solid rectangle lies in the use of conditional checks within the inner loop. In a hollow rectangle (Pattern 2), the statement 'if(i==1 || i==n || j==1 || j==n)' determines whether to print an asterisk, leading to filled borders only, whereas a solid rectangle (Pattern 1) prints an asterisk for every position without conditions. This condition results in the edges being outlined with asterisks while leaving the center empty for hollow rectangles .

The Solid Butterfly (Pattern 13) is realized through manipulation of row and column indices to fill stars symmetrically across a center axis. For each 'i', stars print up to 'i' and also from 'i' after a central gap of '2*(n-i)'. The pattern symmetry relies on duplicating the row structure both upwards and downwards from the center, leading to a butterfly effect. Thus, the star printing sequence uniformly transitions from rows 1 through n and repeats the formially to maintain overall symmetry .

The Hollow Butterfly pattern (Pattern 15) achieves symmetry by mirroring its design vertically and maintaining a consistent horizontal span across its upper and lower halves. Hollowness is introduced through selective asterisk printing only at border lines, controlled by checking 'j == 1 || j == i' within the loops. The central gap of spaces at '2*(n-i)' further emphasizes its symmetrical appearance and hollow nature. These elements ensure a balance between symmetrical form and internal emptiness, essential for visualizing a butterfly structure .

The Number Pyramid (Pattern 11) uses the outer loop index 'i' to define both the position and value being printed in each row. For each row 'i', initial spaces align the numbers centrally, and the numbers themselves are printed in sequence matching the row index, ensuring that the number 'i' appears 'i' times. This means the value and count per row both increase in tandem with 'i', creating a hierarchical structure numerically by row, leading to a straightforward but powerful visual pyramid of numbers .

Floyd's Triangle (Pattern 8) uses a single variable 'k' initialized to zero to track the number count incrementally across rows. It prints numbers in increasing order for each dot position within each row. The outer loop manages the rows while the inner loop assigns values to each position, incrementing 'k' and printing it immediately. This ensures that the numbering continues seamlessly from the previous row's last number, systematically creating an incrementally filled triangle. The complexity arises from managing the sequence and maintaining flow across varying row lengths .

Both the Half Pyramid (Pattern 3) and Inverted Half Pyramid (Pattern 4) structures start with a single star, but they differ in their construction direction. The Half Pyramid pattern prints lines where each subsequent row increases in printed asterisks linearly, controlled by nested loops where the inner loop runs up to the current row index 'i'. The Inverted Half Pyramid, however, starts from 'n' stars and decreases the number of printed stars with each proceeding line, as it iterates backwards from 'n' to 1. These patterns exhibit symmetry in their logic but opposite in order .

In the Palindromic Number Pyramid (Pattern 12), numeric palindromes arise by first printing numbers decreasing from 'i' to 1, then extending the sequence by increasing from 2 to 'i'. The loop structure iterates twice: initially, it decrements from 'i' to 1, and then, the latter part of the loop increments from 2 back to 'i'. This setup inherently mirrors numbers around a midpoint, ensuring that each row forms a symmetric palindrome centered on '1', achieving the intended pattern .

You might also like