We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. C
  3. Conditionals and Loops
  4. Printing Pattern Using Loops
  5. Discussions

Printing Pattern Using Loops

Problem
Submissions
Leaderboard
Discussions

Sort 832 Discussions, By:

recency

Please Login in order to post a comment

  • pharrio
    13 hours ago+ 0 comments
    int main() 
    {
    
        int n;
        scanf("%d", &n);
        int arr[2*n-1][2*n-1];
        
        // initialize all the array with n
        // then replace everything except for the outer ring with n - 1 ...
        // until reaching 1
        for (int j = 0; j < n; j++){
            for (int i = j; i <= 2*n - 2 - j; i++){
                for (int k = j; k <= 2*n - 2 - j; k++){
                    arr[i][k] = n - j;
                }
            }
        }
        
        // print the array    
        for (int i = 0; i < 2*n - 1; i++){
            for (int j = 0; j < 2*n -1; j++){
                printf("%d ", arr[i][j]);
            }
            printf("\n");
        }
        
        return 0;
    }
    
    0|
    Permalink
  • jpriyaragini
    2 days ago+ 0 comments

    include

    include

    include

    include

    int main() {

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

    }

    0|
    Permalink
  • evi_kristo
    5 days ago+ 0 comments

    include

    include

    include

    include

    int main() {

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

    }

    0|
    Permalink
  • h226320046
    5 days ago+ 0 comments

    include

    include

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

    for(int i=1;i<=2*n-1;i++)
    {
        for(int j=1;j<=2*n-1;j++)
        {
            if(j<n-abs(n-i)){printf("%d ",a);a--;}
            else if(j>n+abs(n-i)){a++;printf("%d ",a);}
            else printf("%d ",a);
        }
        printf("\n");
    }
    

    }

    0|
    Permalink
  • ahmed1151991
    7 days ago+ 1 comment

    include

    include

    include

    include

    int main() {

    int n,num=0;
    

    scanf("%d", &n);

    for(int i=0;i<(2*n)-1;i++) {

    if(i<=n-1){
        for(int j=1, t=0, s=0;j<=(2*n)-1;j++,s++)
            {
            if(j<=n){
                num=n-t;
                if(i!=0&&t<i){t++;}
                printf("%d ",num);
            }
            if(j>n){
    
                if(s>=(2*n)-(i+1)){num++;}
                printf("%d ",num);
            }
    
    
        }
    }
    
    if(i>=n){
    
        for(int j=1, t=0, s=0;j<=(2*n)-1;j++,s++)
            {
            if(j<=n+1){
                num=n-t;
                if(i!=0&&t<(2*n-i-2)){t++;}
                printf("%d ",num);
            }
            if(j>n+1){
    
                if(s>=i+1&&num<n){num++;}
                printf("%d ",num);
            }
        }
    
    }
    
    printf("\n");
    

    }

    return 0; }

    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy