0% found this document useful (0 votes)
10 views22 pages

Java Star Pattern Printing Code

The document contains multiple Java programs that generate various patterns using asterisks, numbers, and characters. Each program is structured with a main method and uses nested loops to print specific patterns to the console. The patterns range from simple shapes like squares and triangles to more complex arrangements involving alternating characters and numbers.

Uploaded by

dhaneshsudha996
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)
10 views22 pages

Java Star Pattern Printing Code

The document contains multiple Java programs that generate various patterns using asterisks, numbers, and characters. Each program is structured with a main method and uses nested loops to print specific patterns to the console. The patterns range from simple shapes like squares and triangles to more complex arrangements involving alternating characters and numbers.

Uploaded by

dhaneshsudha996
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

1)print **** 2)print ####

**** ****
**** ####
**** ****
package sunilkumar; package sunilkumar;

public class Pattern1 public class Pattren2


{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int i,j; int i,j;
for(i=1;i<=4;i++) for(i=1;i<=4;i++)
{ {
for (j=1;j<=4;j++) for (j=1;j<=4;j++)
{ {
[Link]("*"); if(i%2!=0)
} {
[Link]( ); [Link]("#");
} }
} else
} {
[Link] .print("*");
}
}
[Link]( );
}
}
}
3)print *#*# 4)print ****
*#*# ****
*#*# **#*
*#*# ****
package sunilkumar; package sunilkumar;

public class Pattren3 public class Pattren4


{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int i,j; int i,j;
for(i=1;i<=4;i++) for(i=1;i<=4;i++)
{ {
for (j=1;j<=4;j++) for (j=1;j<=4;j++)
{ {
if(j%2==0) if(i==3&&j==3)
{ {
[Link]("#"); [Link]("#");
} }
else else
{ {
[Link] .print("*"); [Link] .print("*");
} }
} }
[Link]( ); [Link]( );
} }
} }
} }
5)print **** 6)print *
**** *
* *
****
package sunilkumar;
package sunilkumar;
public class Pattren6
public class Pattren5
{
{ public static void main(String[] args)
public static void main(String[] args)
{
{
int i,j;
int i,j; for(i=1;i<=3;i++)
for(i=1;i<=4;i++)
{
{ for (j=1;j<=3;j++)
for (j=1;j<=4;j++)
{
{
if(i==j)
if(i==3&&j>=2) {
{
[Link]("*");
[Link](" "); }
}
else
else
{
{ [Link] .print(" ");
[Link] .print("*");
}
} }
} [Link]( );
[Link]( );
}
}
}
}
}

}
7)print ***** 8)print *
* * *
* * *****
* * *
***** *
package sunilkumar; package sunilkumar;

public class Pattren7 public class Pattren8


{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int i,j; int i,j;
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
for (j=1;j<=5;j++) for (j=1;j<=5;j++)
{ {
if(i>=2&&i<=4&&j>1&&j<5) if(i==3||j==3)
{ {
[Link](" "); [Link]("*");
} }
else else
{ {
[Link] .print("*"); [Link] .print(" ");
} }
} }
[Link]( ); [Link]( );
} }
}
} }
}
9)print * * 10)print *
* * **
* ***
* *
* *
****
package sunilkumar; *****
package sunilkumar;
public class Pattren9
{ public class Pattern10
public static void main(String[] args) {
{
int i,j; public static void main(String[] args)
for(i=1;i<=5;i++) {
{ int i,j;
for (j=1;j<=5;j++) for(i=1;i<=5;i++)
{ {
if(i==j||i+j==6) for(j=1;j<=i;j++)
{ {
[Link]("*"); [Link]("*");
} }
else [Link]( );
{ }
[Link] .print(" "); }
}
} }
[Link]( );
}
}

}
11)print ***** 12)print *
**** **
*** ***
**
*
****
package sunilkumar; *****
package sunilkumar;
public class Pattern11
{ public class pattern12
{
public static void main(String[] args) public static void main(String[] args)
{ {
int i,j; int i,j,k;
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
for(j=i;j<=5;j++) for(j=i;j<=5;j++)
{ {
[Link]("*"); [Link](" ");
} }
[Link]( ); for(k=1;k<=i;k++)
} {
} [Link]("*");
}
} [Link]( );
}

}
13)print ***** 14)print *
**** * *
*** * * *
** * * * *
* * * * * *
package sunilkumar;
package sunilkumar;
public class pattern13
{ public class pattern14
public static void main(String[] args) {
{ public static void main(String[] args)
int i,j,k; {
for(i=1;i<=5;i++) int i,j,k;
{ for(i=1;i<=5;i++)
for(j=1;j<=i;j++) {
{ for(j=i;j<=5;j++)
[Link](" "); {
} [Link](" ");
for(k=i;k<=5;k++) }
{ for(k=1;k<=i;k++)
[Link]("*"); {
} [Link]("* ");
[Link]( ); }
} [Link]( );
} }

} }

}
15)print * * * * * 16)print *
* * * * ***
* * * *****
* * *******
* *********
package sunilkumar;
package sunilkumar;
public class Pattern16
public class pattern15 {
{ public static void main(String[] args)
public static void main(String[] args) { {
int i,j,k; int i,j,k,star=1,space=4;
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
for(j=1;j<=i;j++) for(j=1;j<=space;j++)
{ {
[Link](" ");
} [Link](" ");
for(k=i;k<=5;k++) }
{ for(k=1;k<=star;k++)
[Link]("*"); {
} [Link]("*");
[Link]( ); }
} star=star+2;
} space=space-1;
[Link]( );
} }
}
}
17)print ********* 18)print *
******* ***
*****
***** *******
*** *********
* *******
package sunilkumar; *****
***
*
public class Pattern18 package sunilkumar;
{ public class Pattern18
public static void main(String[] args) {
public static void main(String[] args)
{ {
int i,j,k,star=9,space=0; int i,j,k,star=1,space=4;
for(i=1;i<=5;i++) for(i=1;i<=9;i++)
{
{ for(j=1;j<=space;j++)
for(j=1;j<=space;j++) {
{ [Link](" ");
}
for(k=1;k<=star;k++)
[Link](" "); {
} [Link]("*");
for(k=1;k<=star;k++) }
{ if(i<5)
{
[Link]("*"); star=star+2;
} space=space-1;
star=star-2; }
else
space=space+1; {
[Link]( ); star=star-2;
} space=space+1;
}
}
} [Link]( );
}
}
}
19)print ********* 20)print *
******* **
***** ***
***
*
****
*** ***
***** **
******* *
*********
package sunilkumar;
package sunilkumar;
public class Pattern19
{ public class Pattern20
public static void main(String[] args) {
{
int i,j,k,star=9,space=0; public static void main(String[] args)
for(i=1;i<=9;i++) {
{ int i,j,star=0;
for(j=1;j<=space;j++) for(i=1;i<=7;i++)
{
[Link](" "); {
} for(j=1;j<=star;j++)
for(k=1;k<=star;k++) {
{
[Link]("*");
[Link]("*");
} }
if(i<5) if(i<4)
{ {
star=star-2;
space=space+1;
star=star+1;
} }
else else
{ {
star=star+2; star=star-1;
space=space-1; }
}
[Link]( ); [Link]("*");
} }
} }
} }
21)print *
** 22)print *#
*** **##
***###
**** ****####
*** *****#####
** package sunilkumar;
* public class Pattern22
package sunilkumar; {
public class Pattern21
{
public static void main(String[] args)
public static void main(String[] args) {
{ int i,j,k,l,star=1,space=4,ash=1;
int i,j,k,star=1,space=3; for(i=1;i<=5;i++)
for(i=1;i<=7;i++) {
{ for(j=1;j<=space;j++)
for(j=1;j<=space;j++)
{
{
[Link](" "); [Link](" ");
} }
for(k=1;k<=star;k++) for(k=1;k<=star;k++)
{ {
[Link]("*"); [Link]("*");
}
if(i<4)
}
{ for(l=1;l<=ash;l++)
star++; {
space--; [Link]("#");
} }
else star=star+1;
{
star--;
space=space-1;
space++; ash=ash+1;
} [Link]( );
[Link](); }
} }}
}}
23)print ***** 24)print 1
** * 22
* * * 333
***** 4444
package sunilkumar; 55555
package sunilkumar;
public class Pattern23
{ public class Pattern24
public static void main(String[] args) {
{
int i,j; public static void main(String[] args)
for(i=1;i<=5;i++) {
{ int i,j;
for (j=1;j<=5;j++) for(i=1;i<=5;i++)
{ {
if(i>=2&&i<=4&&j>1&&j<5&&i!=j) for(j=1;j<=i;j++)
{ {
[Link](" "); [Link](i);
} }
else [Link]( );
{ }
[Link] .print("*"); }
}
} }
[Link]( );
}
}
}
25)print 1 26)print 1
12 23
123 456
1234 package sunilkumar;
12345
package sunilkumar; public class Pattern26
{
public class Pattern25
{ public static void main(String[] args)
{
public static void main(String[] args) int i,j;int c=0;
{ for(i=1;i<=3;i++)
int i,j; {
for(i=1;i<=5;i++) for(j=1;j<=i;j++)
{ {
for(j=1;j<=i;j++) c++;
{ [Link](c);
[Link](j); }
} [Link]( );
[Link]( ); }
} }
}
}
}
27)print 1 28)print 11111
22 2222
333 333
4444 44
55555 5
package sunilkumar;
package sunilkumar;
public class pattern27
{ public class pattern28
public static void main(String[] args) {
{ public static void main(String[] args)
int i,j,k; {
for(i=1;i<=5;i++) int i,j,k;
{ for(i=1;i<=5;i++)
for(j=i;j<=5;j++) {
{ for(j=1;j<=i;j++)
[Link](" "); {
} [Link](" ");
for(k=1;k<=i;k++) }
{ for(k=i;k<=5;k++)
[Link](i); {
} [Link](i);
[Link]( ); }
} [Link]( );
}
} }

} }
29)print 123 30)print 1
456 10
789 101
package sunilkumar; 1010
10101
public class numpattern29 package sunilkumar;
{
public class numpattern30
public static void main(String[] args) {
{ public static void main(String[] args)
int i,j,count=0; {
for(i=1;i<=3;i++) int i,j;
{ for(i=1;i<=5;i++)
for(j=1;j<=3;j++) {
{ for(j=1;j<=i;j++)
count++; {
[Link](count); if(j%2!=0)
{
} [Link](1);
[Link](); }
} else
} {
} [Link](0);
}
}
[Link]( );
}

}
31)print 1 32)print ABC
01 DEF
001 GHI
0001 package sunilkumar;
00001
package sunilkumar; public class Alppattern32
{
public class numpattern31 public static void main(String[] args)
{ {
public static void main(String[] args) int i,j;
{ // char c=65;// or
int i,j; char c= 'A';
for(i=1;i<=5;i++) for(i=1;i<4;i++)
{ {
for(j=1;j<=i;j++) for(j=1;j<4;j++)
{ {
if(i==j)
{ [Link](c);
[Link](1); c++;
} }
else [Link]( );
{ }
[Link](0); }
}
} }
[Link]( );
}

}
}
33)print AAA
BBB
CCC package sunilkumar;
package sunilkumar;
public class Alppattern33
public class Alppattern33 {
{
public static void main(String[] args)
public static void main(String[] args) {
{ char i,j;
int i,j; for(i='A';i<='C';i++)
// char c=65;// or {
char c= 'A'; for(j='A';j<='C';j++)
for(i=1;i<4;i++) {
{
for(j=1;j<4;j++) [Link](i);
{ }
[Link]( );
[Link](c); }
} }
c++;
[Link]( ); }
}
}
}

or
34)print ABC 35)print A
ABC BB
ABC CCC
package sunilkumar; DDDD
EEEEE
public class Alppattern34 package sunilkumar;
{
public class Alppattern35
public static void main(String[] args) {
{
char i,j; public static void main(String[] args)
for(i='A';i<='C';i++) {
{ int i,j;
for(j='A';j<='C';j++) char c= 'A';
{ for(i=1;i<=5;i++)
{
[Link](j); for(j=1;j<=i;j++)
} {
[Link]( );
} [Link](c);
} }
c++;
} [Link]( );
}
}

}
36)print A 37)print
BC A
DEF AB
GHIJ ABC
KLMNO package sunilkumar;
package sunilkumar;
public class Alppattaern37
public class Alppattern36 {
{ public static void main(String[] args)
{
public static void main(String[] args) char i,j;
{ for(i='A';i<='C';i++)
int i,j; {
char c= 'A'; for(j='A';j<=i;j++)
for(i=1;i<=5;i++) {
{
for(j=1;j<=i;j++) [Link](j);
{ }
[Link]( );
[Link](c); }
c++;
} }
[Link]( );
} }
}

}
38)print A 39)print 1AAAA
B B 12BBB
C C C 123CC
D D D D 1234D
package sunilkumar; 12345
package sunilkumar;
public class Pattern38
{ public class NUMAPLPATTERN39
public static void main(String[] args) {
{ public static void main(String[] args)
int i,j,k; {
char c= 65; int i,j;
for(i=1;i<=4;i++) char ch='A';
{ for(i=1;i<=5;i++)
for(j=i;j<=4;j++) {
{ for(j=1;j<=5;j++)
[Link](" "); {
} if(i>=j)
for(k=1;k<=i;k++) {
{ [Link](j);
[Link](c+" "); }
// c++; else
} {
c++; [Link](ch);
[Link]( ); }
} }
} [Link]();
ch++;
} }
}
}
40)print ***** 41)print 1
A**** 01
BC*** 101
DEF** 0101
GHIJ* package sunilkumar;
package sunilkumar;
public class NUMAPLPATTERN40 public class Pattern41 {
{
public static void main(String[] args) public static void main(String[] args)
{ {
int i,j; int i,j;
char ch='A'; for(i=1;i<=4;i++)
for(i=1;i<=5;i++) {
{ for(j=1;j<=4;j++)
for(j=1;j<=5;j++) {
{ if(i>=j)
if(i>=j) {
{
[Link](j); if(i%2==0&&j%2!=0||i%2!=0&&j%2==0)
} {
else [Link](0);
{ }
[Link](ch); else
ch++; {
} [Link](1);
} }}}
[Link](); [Link]( );
}
} }
} }
}
42)print 1
222
33333
4444444
package sunilkumar;

public class Pattern10


{
public static void main(String[] args)
{
int i,j,k,star=1,space=4;
for(i=1;i<=5;i++)
{
for(j=1;j<=space;j++)
{

[Link](" ");
}
for(k=1;k<=star;k++)
{
[Link](i);
}
star=star+2;
space=space-1;
[Link]( );
}
}
}

Common questions

Powered by AI

Pattern41 implements alternating binary values using nested loops where conditions check parity with modulo operations. The outer loop increments through rows, while the nested loop processes columns. The pattern alternates values based on even or odd conditions for both row and column indices, creating a chessboard-style binary pattern .

Pattern13 controls space and asterisk placement through nested loops. The inner loop initially prints spaces equal to the row number, pushing stars to the right. Subsequently, stars are printed starting from the current row to the pattern's limit, creating a staircase or inverted half-pyramid aligned to the right. The configuration ensures each line shifts by one to the right with decreasing asterisks, thus achieving the desired right-aligned descending pattern .

Pattern35 incrementally builds its alphabetic pattern by iterating over rows, incrementing both the character and its repetitions per line. The outer loop tracks rows, while the inner loop uses a counter to print each character 'A' to 'E' progressively, with each row featuring increasing repetitions of its line's letter .

Pattern9 forms a symmetrical X-shape using a logic-based approach that checks two conditions: if the current row index 'i' equals the column index 'j' or if their sum equals six, printing an asterisk in these cases creates diagonals crossing each other symmetrically from corner to corner .

Pattern11 produces a simple descending pattern of stars decreasing left to right, with each subsequent line containing one less star than the previous line via direct controls within a single nested loop structure. Meanwhile, Pattern13 integrates space adjustment by adding leading spaces before stars, further patched by nested loops managing decreasing star count per line, thus creating a right-aligned pattern. The main distinction lies in Pattern11's straightforward decrement of stars versus Pattern13's additional step of space insertion for visual complexity .

Pattern16 implements a pyramid structure using nested loops. Key elements include initializing variables 'star' and 'space' which control the number of asterisks and spaces, respectively. The outer loop iterates over rows, while inner loops manage spaces and stars. For each row, it decreases 'space' by 1 and increases 'star' by 2, creating a centered effect of increasing number of asterisks per line .

Pattern18 uses a single for-loop with a conditional statement to modify 'star' and 'space', producing a full diamond in a single loop pass. In contrast, Pattern19 manipulates 'star' and 'space' differently by splitting the patterns into two consecutive structures (a downward and upward structure) and alternately increasing and decreasing 'star' and 'space' counts. The main difference lies in Pattern19 fully separates the expansion and contraction phases into different halves, while Pattern18 dynamically adjusts within one continuous loop .

Pattern29 utilizes a counter, 'count', initialized before nested loops to generate a consistent numeric progression across its rows. It increments 'count' within a fixed-size grid structure (3x3 matrix), printing each value sequentially. The method systematically fills each row linearly through layered loop logic ensuring orderly progression without restarting counters per row .

Pattern30 uses binary conditions within nested loops to print sequences of alternating 1s and 0s. Within each row, it evaluates column parity, printing '1' for odd and '0' for even positions. This approach systematically generates alternating sequences specific to current loop indices, reflecting binary symmetry in increasing line lengths .

Pattern24 uses simple nested loops without explicit conditionals to create patterns with numbers where each line has a consecutive repetition of the line number. The outer loop controls the lines, and the inner loop dictates the repetition of the number according to the current line number. This highlights a pattern without complex logic, focusing on incrementing iterations instead .

You might also like