Printing Pattern Using Loops

Sort by

recency

|

1062 Discussions

|

  • + 0 comments

    my background is not CS! so I was stuck, bruteforcing this problem for a week until I asked the right questions and this popped up! https://www.geeksforgeeks.org/machine-learning/chebyshev-distance/

  • + 0 comments

    It’s a great exercise for understanding loop control, indexing, and pattern logic in programming! Play Exchange Login ID and Password

  • + 0 comments

    Printing Pattern Using Loops

  • + 0 comments

    Working with conditionals and loops on HackerRank is such a great way to sharpen problem-solving skills. Playingexchange

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() 
    {
    
        int n;
        scanf("%d", &n);
        int arr[2*n-1];
      	for(int i = 0; i<2*n-1;i++){
            arr[i] = n;
        }
        
        for(int i = 0,k = 2;i<2*n -1;i++){
            if( i > 2*n/2){
                k++;
            }
            for(int j = 0; j<(2*n -1);j++){
                if((j>=i )&& (j<((2*n-1)-(i))) && i < (2*n)/2)
                    arr[j] = n-i;
                else if (j >= ((2*n-1)-(i+1)) && (j < i))
                    arr[j] = k;
                (j != 2*n-2)? printf("%d ",arr[j]) : printf("%d",arr[j]);
            }
            printf("\n");
        }
        
        return 0;
    }