Printing Pattern Using Loops

Sort by

recency

|

1079 Discussions

|

  • + 0 comments

    I decided that i am not going to use ai so i thinked how to solve and tried and run each time upadating code in onlinegdp compiler then i got answer after three and half hour(3:30) so it take so much time but i learn very much

    include

    include

    include

    include

    int numberOfLine(int a){ if(a==1) return 1; else if(a==2) return 3; else return (a+a)-1;

    }

    int main() {

    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    int c=n,c1=n;
    int num=numberOfLine(n);
    for(int i=(num/2)+1;i>0;i--){ 
    for(int j=n;j>=c;j--){
        printf("%d",j);}
        for(int k=num-2;k>0;k--){
            printf("%d",c1);
    
        }num-=2;
    
        for(int j=c;j<=n;j++){
            if(j!=1)
                printf("%d",j);
    
        }printf("\n");c1--;
        c--;
    
    }
    c=2,c1=2;
     num=numberOfLine(n);
     int temp=1;
    for(int i=(num/2);i>0;i--){ 
    for(int j=n;j>=c;j--){
        printf("%d",j);}
        for(int k=temp;k>0;k--){
            printf("%d",c1);
    
        }temp+=2;
    
        for(int j=c;j<=n;j++){
            if(j!=1)
                printf("%d",j);
    
        }printf("\n");c1++;
        c++;
    
    }
    
    
    return 0;
    

    }

  • + 0 comments

    My approach is that, since the figure is symmetrical, I solve the upper-left part and then mirror it to the right, downward, and to the lower-right quadrant. Although it’s not particularly elegant.

    int main() {

    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    int pattern[2*n-1][2*n-1];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            pattern[i][j] = n - (i <= j ? i : j);
            pattern[2*n-2-i][j] = pattern[i][j];
            pattern[i][2*n-2-j] = pattern[i][j];
            pattern[2*n-2-i][2*n-2-j] = pattern[i][j];
        }
    }
    for (int i = 0; i < 2*n-1; i++) {
        for (int j = 0; j < 2*n-1; j++) {
            printf("%d ", pattern[i][j]);
        }
        printf("\r\n");
    }
    return 0;
    

    }

  • + 0 comments

    int main() {

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

    }

  • + 0 comments

    I tried it for more than half an hour could not solve it so turned to ChatGPT. Then i understood it. 1. First make variables to store the distance of a particular position from all the borders(top, left, right, bottom). 2. then make a new variable(I named it min_dist) to store the minimum distance from any border. 3. Make any distance from any border as min_dist. then check if the distances are lower than the min_dist. If lower then set that distance as new min_dist. 4. Lastly, print the value of that position which is (n-min_dist) HOW? See this structure as a square , the minimum distance from the outermost square to the outermost square is o ,SO,the outermost square is made of the integer n itself. and then when we go to the lower square the minimum distance from the outermost square becomes 1 ,SO,this square is made of (n-1) i.e (n-min_dist) and the same goes on for all the remaining inner squares.

    I HOPE I EXPLAINED IT WELL

    include

    int main() {

    int n;
    scanf("%d", &n);
    
    int size=(2*n-1);
    
    for(int i=0;i<(2*n-1);i++){
        for(int j=0;j<(2*n-1);j++){
            int top=i;
            int left=j;
            int right=(size-1)-j;
            int bottom=(size-1)-i;
    
            int min_dist=top;
            if(bottom < min_dist){min_dist=bottom;}
            if(left < min_dist){min_dist=left;}
            if(right < min_dist){min_dist=right;}
            printf("%d\t",n-min_dist);
        }
        printf("\n");
    }
    return 0;
    

    }

  • + 0 comments

    I modified the way to represent absolute value f(x) = |x| for easier implementation.

    int main() 
    {
    
        int n;
        scanf("%d", &n);
      	// Complete the code to print the pattern.
        int width = 2*n-1;
        int min, cur;
        for (int h = 0; h < width; h++){
            min = (n - h > 2-n + h) ? n - h : 2-n + h;
            for (int w = 0; w < width; w++){
                cur = (n - w > 2-n + w) ? n - w : 2-n + w;
                printf("%d " , (cur > min) ? cur : min);
            }
            printf("\n");
            
        }
        return 0;
    }