OneCompiler

alternate row printing box pattern

114


Alternate row printing box pattern

i/p:
6
o/p
1 2 3 4 5 6
13 14 15 16 17 18
25 26 27 28 29 30
31 32 33 34 35 36
19 20 21 22 23 24
7 8 9 10 11 12

Following is sample C code.

#include <stdio.h>
int main() {
    int n = 6;
    // scanf("%d",&r);
    int s=1;
    int a[100][100];
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        a[i][j]=0;
      }
    }
    int k=n*2;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(i==n/2){
          a[i][j]=a[i-1][j]+n;
        }
        else if(i==0){
          a[i][j]=s+j;
        }
        else if(i>n/2){
          a[i][j]=a[i-1][j]-k;
        }
        else{
          a[i][j]=a[i-1][j]+k;
        }
        printf("%d ",a[i][j]);
      }
      printf("\n");
    }
    // for(int i=0;i<n;i++){
    //   for(int j=0;j<n;j++){
    //     printf("%d ",a[i][j]);
    //   }
    //   printf("\n");
    // }
    // printf("%d",6/2);
}