Recent Posts

Thursday 18 June 2015

15:42
Hello Dear Readers,
After a long while, I have managed some time to maintain this blog with useful post.  Apologizes, for inactive mode.
Without wasting time, Let’s start writing codes….
We need to print the Diamond like structure on screen . So firstly we need to analyze the logic that will lead to print the such structure.
I’ll recommend you to please use Pen-Notebook or text-editor like notepad (only If you are comfort with with it) to draw the diamond structure.
Then analyze the its symmetry and pattern.
                               

Here is the codes…

 /*
@author therootcoder
*/

public class PrintDiamond{
public static void main(String[] args) {
int i,j,z=1,k,firstLoop;
for(i=1;i<=9;i++){
j=1;
if(i<5){
firstLoop=5-i;
}else{
firstLoop=i-5;
}
// this loop is being used to print the white space before starting of stars in every row  (Top to bottom)
while(j<=firstLoop){
System.out.print(" ");
j+=1;
}
// this logic  will manage the no stars being printed on the screen (Top to bottom)
if(i>5)
z=10-i;
else
 z=i;
k=1;
while(k<=z){
System.out.print("* ");
k+=1;
}
System.out.println();
}
}
}


Output:
PrintDiamond Program output

0 comments:

Post a Comment