Printing Pattern Using Loops

  • + 84 comments

    my solution:

    #include <stdio.h>
    
    int main() 
    {
    
        int n;
        scanf("%d", &n);
        int len = n*2 - 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;
    }
    

    Get the min distance to the four sides, wich is the differ between n and the number we wanted.