Printing Pattern Using Loops

Sort by

recency

|

1056 Discussions

|

  • + 0 comments

    int main() {

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

    }

  • + 0 comments
    • i have used some conditions and a variable which is inc-dec .. how do you see this solution?

    • int a;

    • int i,j;
    • scanf("%d", &a);
    • int n=(a*2)-1;
    • for(i=1;i<=n;i++)
    • {
    • for(j=1;j<=n;j++)
    • {
    • printf("%d ",((i-j>0&&i<(n/2)+1)||(i+j=(n/2)+1))?a--:((i-j<0 &&i>(n/2)+1)||(i+j>n+1 && i<=(n/2)+1))?++a:a);
    • }
    • printf("\n");
    • }
  • + 0 comments

    At first, I created the matrix for the first quadrant and used a lot of lines to print it in order and reverse order. But later, I saw that the AI's code was much more concise. That's when I realized the pattern: the value of each element is n - d, where d is the minimum distance from the element to the matrix boundary.

  • + 0 comments

    Here is Printing patterns using loops solution in c - https://programmingoneonone.com/hackerrank-printing-pattern-using-loops-in-c-solution.html

  • + 0 comments

    include

    include

    include

    include

    int main() {

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

    }