# Diamond perimeter with loop

• November 16th, 2011, 01:42 PM
Lanto
Diamond perimeter with loop
Hi. I've beed trying to do this for days but I can't.

http://img831.imageshack.us/img831/8334/diamondc.png

I got the program to do the whole diamond but I don't know how to take out all the asterisk. żAny idea?

This is my code:

Code :

```public class diamond { public static void main(String args[]) { int i=0,j,k,n=4; for(k=1;k<=n;k++) { for(i=0;i<=n-k;i++) { System.out.print(" "); } for(j=1;j<=k;j++) { System.out.print("*"); } for(j=1;j<=k-1;j++) { System.out.print("*"); } System.out.println(""); } for(k=1;k<=n;k++) { for(i=0;i<=k;i++) { System.out.print(" "); } for(i=1;i<=n-k;i++) { System.out.print("*"); } for(j=0;j<=n-k-2;j++) { System.out.print("*"); } System.out.print("\n"); } } }```
• November 16th, 2011, 04:47 PM
Junky
Re: Diamond perimeter with loop
Apart from the first and last line, each line has: N spaces, star, M spaces, star. All you have to do is work out what N and M is for each line.
• November 17th, 2011, 06:09 AM
Mr.777
Re: Diamond perimeter with loop
Inside the outer loop, keep a flag, which will keep track if the very first * has been printed, don't let the program draw * again, unless it's the last index of inner loop.
Note: Don't forget to reset the flag.
• August 9th, 2012, 03:46 AM
Rage1337
Re: Diamond perimeter with loop
Quote:

Originally Posted by Mr.777
Inside the outer loop, keep a flag, which will keep track if the very first * has been printed, don't let the program draw * again, unless it's the last index of inner loop.
Note: Don't forget to reset the flag.