jeudi 26 octobre 2017

c language : how to get a number (n) and print 1 to n*n in form of a square

I want to produce an output like this:

get number n>=2

n=2 :

1 2 4 3 n=3 :

1 2 3 8 9 4 7 6 5

n=4 : 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7

first we go around nn square then (n-2)(n-2) etc...

#include <stdio.h>
int main(int argc, char *argv[]) {
int n;
scanf("%d",&n);
int x = n;

int table[n][n]; 

int i,j;

//initializing array with 0
for(i=0 ; i<n ; i++){
    for(j=0 ; j<n ; j++){
        table[i][j]=0;
    }
}



//printing array
for(i=0 ; i<n ; i++){
    for(j=0 ; j<n ; j++){
        printf("%d\t",table[i][j]);
    }
    printf("\n");
  }
return 0;
}

please help me how to solve this ...

Aucun commentaire:

Enregistrer un commentaire