mercredi 25 novembre 2020

How to make diamond pattern using java?

How to make this pattern if input N = 5 Output : diamond

Mine is like this, it become 2N if input N = 5 Output my diamond

Here's my code

int i,j;

    for(i = 0; i <= n; i++)
    {
        for(j = 1; j <= n - i; j++)
            System.out.print(" ");
        for(j = 1; j <= 2 * i - 1; j++)
            System.out.print("*");
        System.out.print("\n");
    }

    for(i = n - 1; i >= 1; i--)
    {
        for(j = 1; j <= n - i; j++)
            System.out.print(" ");
        for(j = 1; j <= 2 * i - 1; j++)
            System.out.print("*");
        System.out.print("\n");
    }

What should i fix??

Aucun commentaire:

Enregistrer un commentaire