[Go to site: main page, start]

0% found this document useful (0 votes)
32 views17 pages

Java Patterns: 27 Coding Examples

Uploaded by

Sanjib Das
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)
32 views17 pages

Java Patterns: 27 Coding Examples

Uploaded by

Sanjib Das
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

Ans.

class Question69
{
static void pattern_i()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](j);
}
[Link]();
}
}
static void pattern_ii()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_iii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_iv()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_v()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_vi()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](i);
}
[Link]();
}
}
static void pattern_vii()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
[Link](i);
}
[Link]();
}
}
static void pattern_viii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
[Link](i);
}
[Link]();
}
}
static void pattern_ix()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
[Link](i);
}
[Link]();
}
}
static void pattern_x()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link]("*");
}
[Link]();
}
}
static void pattern_xi()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
[Link]("*");
}
[Link]();
}
}
static void pattern_xii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(i==j || i+j==6)
[Link]("*");
else
[Link](" ");
}
[Link]();
}
}
static void pattern_xiii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if((i+j)%2!=0)
[Link](0);
else
[Link](1);
}
[Link]();
}
}
static void pattern_xiv()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
[Link](j);
}
[Link]();
}
}
static void pattern_xv()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(i+j>=6)
[Link](j);
else
[Link](" ");
}
[Link]();
}
}
static void pattern_xvi()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=i;j>=1;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_xvii()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=1;j<=i;j++)
{
if(i==j || j==1)
[Link](1);
else
[Link](0);
}
[Link]();
}
}
static void pattern_xviii()
{
int i,j,a,b,c;
for(i=1;i<=6;i++)
{
a=1;b=0;
for(j=1;j<=i;j++)
{
c=a+b;
[Link](c);
a=b;
b=c;
}
[Link]();
}
}
static void pattern_xix()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=i;j<=5;j++)
{
[Link](" ");
}
for(j=1;j<=i;j++)
{
[Link](j);
}
for(j=i-1;j>=1;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_xx()
{
int i,j;
for(i=6;i>=1;i--)
{
for(j=i;j<6;j++)
{
[Link](" ");
}
for(j=i;j>=1;j--)
{
[Link](j);
}
for(j=2;j<=i;j++)
{
[Link](j);
}
[Link]();
}
}
static void pattern_xxi()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j>=i)
[Link](j);
else
[Link](" ");
}
[Link]();
}
}
static void pattern_xxii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if((j+i)%2==0)
[Link](1);
else
[Link](0);
}
[Link]();
}
}
static void pattern_xxiii()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=1;j<=9;j+=2)
{
if(j>=i)
[Link](10-i);
else
[Link](" ");
}
[Link]();
}
}
static void pattern_xxiv()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=1;j<=9;j+=2)
{
if(j+i>=10)
[Link](i);
else
[Link](" ");
}
[Link]();
}
}
static void pattern_xxv()
{
/*
12345
23451
34512
45123
51234
*/
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
[Link](j);
}
for(j=1;j<=i-1;j++)
{
[Link](j);
}
[Link]();
}
}
static void pattern_xxvi()
{
/*
54321
43215
32154
21543
15432
*/
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
[Link](j);
}
for(j=5;j>=i+1;j--)
{
[Link](j);
}
[Link]();
}
}
static void pattern_xxvii()
{
/*
12345
51234
45123
34512
23451
*/
int i,j;
for(i=5;i>=1;i--)
{
for(j=i+1;j<=5;j++)
{
[Link](j);
}
for(j=1;j<=i;j++)
{
[Link](j);
}
[Link]();
}
}
}

Common questions

Powered by AI

Pattern_xxii employs nested loops combining i and j indexes which impact printing binary numbers based on whether their sum is even or not, triggering a checkerboard appearance (alternating pattern). Utilizing modulus operations accomplishes this through logical checks on summation ensuring alternating entries .

Pattern_xix forms an ascending pyramid where spaces and increasing numbers first appear with a mirrored set of decreasing numbers. The mirrored effect is achieved by reversing the loop order to first decrease, then increase from middle; while pattern_xx does this in reverse order to form a descending set, using spaces for alignment .

Pattern_xiii uses a logic where positions are filled with '0' if the sum of the row (i) and column (j) indexes is odd, and '1' if even. This results in a checkerboard-like pattern in each row, maintaining symmetry along the central line due to consistent alternation of values .

Pattern_xi makes use of nested loops to decrement the number of stars printed in each line by decreasing the loop range. For pattern_xvii, nested loops are used to print ones at specific positions (beginning and end of the number sequence), introducing zeros everywhere else. The logic is centered around combination indexes producing specific numbers, demonstrating nested loops’ flexibility in pattern diversity .

Pattern_xviii stands out by incorporating an algorithm reminiscent of Fibonacci sequence generation. A variable update mechanism within a nested loop generates numbers by summing the prior two entries (a=prior, b=current, c=sum), creating a mathematical pattern rather than a straightforward repeat or increment seen in other patterns .

Pattern_xiv utilizes decremental loops within the starting point to decrease the number’s value on each sequential row, which creates a stepped visual dropping off high numbers leading in each line. This effect visually suggests a downward gradual shift in sequence even though numbers themselves seen only linearly vary in the loop .

Pattern_xxv and pattern_xxvii differ in terms of initial number sequence but share a cycling logic. Pattern_xxv starts rows from the ongoing highest number with cyclic increments to complete the count; meanwhile, pattern_xxvii begins from the prior highest number and completes with earlier numbers to fill lines, producing a reversed start similar cycling mechanism .

Pattern_xxv demonstrates number rotation within fixed bounds by repeatedly cycling through sequences up to five numbers. This offers learners insight into cyclic iteration, teaching how numbers realign after borderline, a foundational concept in understanding modular arithmetic and cyclic algorithms .

Pattern_xvi maintains descending order by utilizing inner loops that decrement from the ongoing sequence top, adhering to the decrease set by the upper loop constraint. This orderly reduction makes the decrementing pattern evident and consistent across pattern lines .

Pattern_x generates a pyramid style pattern with increasing stars on each line, starting from one ‘*’ and going up to five ‘*’s. Conversely, pattern_xi generates an inverted pyramid pattern with decreasing stars, starting with five ‘*’s and decrementing to one. This difference results in pattern_x having an upright triangular shape and pattern_xi an inverted triangle .

You might also like