Print diamond shape in Java I'm printing a diamond shape in Java. It's working fine, but is there a way to re-write this code to make it faster?

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

sc.close();

String ns = String.valueOf(n);
int z = n/2;

int k = n%2;


for (int j = 0 ; j&lt;n;j++) {

    for (int i = 0; i &lt; z; i++)
        System.out.print(" ");
    for (int i = 0; i &lt; k ; i++){
        System.out.print("*");
    if (i==k-1)
        System.out.print("\n");
    }

    if ((k-n)==0)
        break;
    z--;
    k=k+2;

}

 z = (n)/2;

 k = (n)%2;
int w = k;
int y = n-2;

for (int j = 0 ; j&lt;z  ;j++) {

    for (int i = 0 ; i&lt;w;i++)
    System.out.print(" ");


    for (int i = 0 ; i&lt; y ;i++)
        System.out.print("*");
    w=w+1;
    y=y-2;

    System.out.print("\n");

}
After reviewing your code, I'll point out that:

It would be better if you used more descriptive variable names. It improves readability and might even help you better understand your own code, ex:

int width = sc.nextInt(); // or whatever it represents


Always use curly braces for if and for statements, even if they wrap around a single statement. Shorter code isn't faster, but makes it easier to introduce bugs. You never pay a penalty for extra curly braces or parentheses, so use them as much as you want to clarify intention. For example this snippet can be ambiguous:

for (int i = 0 ; i&lt; y ;i++)
    System.out.print("*");
w=w+1;
y=y-2;


Did you mean

for (int i = 0 ; i&lt; y ;i++) {
    System.out.print("*");
    w=w+1;
    y=y-2;
}


or

for (int i = 0 ; i&lt; y ;i++) {
    System.out.print("*");
}
w=w+1;
y=y-2;