Printing Pattern Using Loops

Sort by

recency

|

1019 Discussions

|

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

  • + 0 comments

    include

    include

    include

    include

    int main() {

    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    int arr_siz = ((n-1)*2) + 1;
    int **arr = (int**)malloc(arr_siz * sizeof(int*));
    for (int i = 0; i < arr_siz; i++)
        arr[i] = (int*)malloc(arr_siz * sizeof(int));
    
    int arr_loop = (arr_siz/2) + 1;
    int num_dec = 0;
    for(int i =0; i < arr_loop; i++)
    {
        int  dec_val = 0;
        num_dec = i;
        for(int j=0; j< arr_loop; j++)
        {
            arr[i][j] = n-dec_val;
            arr[i][arr_siz -1 - j] = n-dec_val;
            if(num_dec)
            {
                dec_val += 1;
                num_dec--;
            }
        }
        memcpy(arr[arr_siz-1-i], arr[i],arr_siz * sizeof(int));
    }
    for (int i = 0; i < arr_siz; i++)
    {
        for (int j = 0; j < arr_siz; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }       
    
    /* Code for further processing and free the
       dynamically allocated memory */
    
    for (int i = 0; i < arr_siz; i++)
        free(arr[i]);
    return 0;
    

    }

  • + 0 comments

    include

    include

    include

    include

    int main() {

    int n;
    scanf("%d", &n);
    int min=0;
    for(int i=1;i<=2*n-1;i++){
        for(int j=1;j<=2*n-1;j++){
            int a = i;
            if(i>n) a = 2*n - i;
            int b = j;
            if(b>n) b = 2*n - j;
            if(a<b) min = a;
            else min = b;
            printf("%d ",n+1-min);
        }
        printf("\n");
    }
    // Complete the code to print the pattern.
    return 0;
    

    }