+ 2

how to make a star pattern in java

26th Aug 2016, 2:08 PM
Pradeep Saini
Pradeep Saini - avatar
4 Answers
+ 1
use for loop
26th Aug 2016, 7:09 PM
Maske Uttam Prabhakar
Maske Uttam Prabhakar - avatar
+ 1
* *** ***** ******* ***** *** * we could use recursive function to atchieve that. try the following program in your PC ex: public class Program { public static void drawDiamond(int levels){ drawDiamond(levels,1,false); } private static void drawDiamond(int levels,int it,boolean reverse){ if(it==0) return; if(it>levels){ reverse =true; it=it-2; } if(it<levels){ System.out.print(String.format("%"+(levels-it)+"s","")); } int stars=it*2-1; for(int i=0;i<stars;i++){ System.out.print("*"); } System.out.println(); if(reverse){ drawDiamond(levels,--it,true); }else{ drawDiamond(levels,++it,false); } } public static void main(String[] args) { drawDiamond(4); } }
27th Aug 2016, 7:44 AM
Tiger
Tiger - avatar
- 1
can you give details on what star pattern
26th Aug 2016, 6:07 PM
Gouda Kiran
Gouda Kiran - avatar
- 1
a dimond shape
27th Aug 2016, 1:40 AM
Pradeep Saini
Pradeep Saini - avatar