Printing Pattern Using Loops

Sort by

recency

|

1021 Discussions

|

  • + 0 comments
    
    
    scanf("%d", &n);
    // Complete the code to print the pattern.
    for(i=0;i<n;i++){
        temp=n;
        for(j=0;j<n;j++){
          printf("%d ",temp);
          if(j<i)
            temp--;
        }
        for(j=(n-2);j>0;j--){
          if(j<i)
            temp++;
            printf("%d ",temp);
        }
        printf("%d\n",n);
    }
    for(i=0;i<(n-1);i++){
        temp=n;
        for(j=n;j>0;j--){
          printf("%d ",temp);
          if((i+2)<j)
            temp--;
        }
        for(j=0;j<(n-2);j++){
          if(i<j)
            temp++;
          printf("%d ",temp);
        }
        printf("%d\n",n);
    }
    

    `

    
    
    
    
  • + 0 comments

    int t=0,s=0;

      for(int i=1; i<n*2; i++)
      {
    
          for(int j=0; j<n*2-1; j++){
    
              t=i<=n?i:-i+n+n;
    
              if(t>j) s=n-j;
    
                            else{
                  if(j<n-t+n)  s=n-t+1;
                  else s= j-n+2;
              }
              printf("%d ",s);
          }
          printf("\n");
      }
    
  • + 0 comments

    simple and easy answer

    for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                int min = (i<j?i:j)<(size-i-1<size-j-1?size-i-1:size-j-1)?(i<j?i:j):(size-i-1<size-j-1?size-i-1:size-j-1);
                printf("%d ", n - min);
            }
            printf("\n");
    

    takes the minimum out of i,j,size-i-1 and size-j-1, where size is 2n-1

  • + 0 comments

    core component of this code: for(int x=0;x=n;x--) { for(int y=2*(n-1)-z;y>=z;y--){ a[x][y]=n-z; a[y][x]=n-z; } z++;}

  • + 0 comments

    include

    void main( ) { int n; printf("Give input"); scanf("%i",&n); for(int i=1; i <= 2*n-1;i++) { int k=1; for(int j=1;j <= 2*n-1; j++) { for(int m=1;mi) k --; if(j>=2*n-i+1) k --; break; } printf("%i ",n-k+1); } printf("\n"); } }