Java Coding practices-STAR Pattern
- Anand Nerurkar
- Nov 29, 2023
- 1 min read
Updated: Jan 6, 2024
solve any star pattern coding priblem, below are the common steps
no of line=no of rows=no of outer loop
identify for each row, no of cols-- for (int row=1;row<=n;row++){for(int col=1;col<=row;col++)
what we need to print say *
print below pattern

for(int row=1;row<=4;row++){
for(int col=1;col<=row;col++){
System.out.println("* ");
}
System.out.println(" ");
}
print below pattern

for(int row=1;row<=4;row++){
for(int col=1;col<=4;col++){
System.out.println("*");
}
System.out.println();
}
print below pattern
/*
88888
8888
888
88
8
*/
int n=5
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n-row+1;col++)
{
sysout("8");
}
sysout("");
}
print below pattern
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/
int n=5;
for(int row=1;row<=n;row++){
for(int col=1;col<=row;col++){
sysout(col);
}
sysout();
}
print below pattern
/*
*----1
* *---2
* * *--3
* *--4
*---5
*/
int n=3;
for(row=1;row<=2*n-1;row++)
{
tcol=row>n? 2*n-row:row;
for(int col=1;col<=tcol;col++){
sysout("*);
}
sysout();
}
Comments