top of page

Java Coding practices-STAR Pattern

  • Writer: Anand Nerurkar
    Anand Nerurkar
  • Nov 29, 2023
  • 1 min read

Updated: Jan 6, 2024

  1. solve any star pattern coding priblem, below are the common steps

  2. no of line=no of rows=no of outer loop

  3. identify for each row, no of cols-- for (int row=1;row<=n;row++){for(int col=1;col<=row;col++)

  4. what we need to print say *


  1. print below pattern


ree

for(int row=1;row<=4;row++){

for(int col=1;col<=row;col++){

System.out.println("* ");

}

System.out.println(" ");

}


print below pattern


ree

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();

}



 
 
 

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2024 by AeeroTech. Proudly created with Wix.com

bottom of page