Printing Pattern Using Loops

Sort by

recency

|

1025 Discussions

|

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
        int n;
        scanf("%d", &n);
    
        int len = 2*n - 1;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                int min = i < j ? i : j;
                min = min < len-i ? min : len-i-1;
                min = min < len-j-1 ? min : len-j-1;
                printf("%d ", n-min);
            }
            printf("\n");
        }
    
        return 0;
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
        int n;
        scanf("%d", &n);
    
        int len = 2*n - 1;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                int min = i < j ? i : j;
                min = min < len-i ? min : len-i-1;
                min = min < len-j-1 ? min : len-j-1;
                printf("%d ", n-min);
            }
            printf("\n");
        }
    
        return 0;
    }
    
  • + 0 comments

    include

    include

    include

    include

    int main() { int n; scanf("%d", &n);

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

    }

  • + 0 comments

    include

    int main() { int n; scanf("%d", &n); for(int i = -n + 1;i <= 0;i++){ for(int j = 0;j < i + n - 1;j++){ printf("%d ", n - j); } for(int j = n + i;j <= n - i;j++){ printf("%d ", -i + 1); } for(int j = i + n - 2;j >= 0;j--){ printf("%d ", n - j); } printf("\n"); } for(int i = 1;i < n;i++){ for(int j = 0;j <= n - 1 - i;j++){ printf("%d ", n - j); } for(int j = n - i;j < n + i;j++){ printf("%d ", i + 1); } for(int j = n - 1 - i - 1;j >= 0;j--){ printf("%d ", n - j); } printf("\n"); } return 0; }

  • + 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);
    }
    

    `