dimanche 5 novembre 2017

how to print this numeric x pattern using for loops in java

1       1
 2     2
  3   3
   4 4
    5
   4 4
  3   3
 2     2
1       1

How to print this pattern using for loops in a optimized form?

int i,j,k=1;
int m[7][7]={0};
for(i=1;i<=7;i++)
{
    for(j=1;j<=7;j++)
    {
        if(j==i || 8-i==j)
        {
            m[i-1][j-1]=k;
        }
    }
    if(i<4)
    {
        k++;
    }
    else
    {
        --k;
    }
}
for(i=0;i<7;i++)
{
    for(j=0;j<7;j++)
    {
        if(m[i][j]==0)
        {
            printf(" ");
        }
        else
        {
            printf("%d",m[i][j]);
        }
    }
    printf("\n");
}

Aucun commentaire:

Enregistrer un commentaire