Printing Pattern Using Loops

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
        int n,k;
        scanf("%d",&n);
        for(int i=0; i<n; i++){
            k = n;
            for(int j=0; j<2*n-1; j++) {
                printf("%d ",k);
                k = (((k-j)>(k-i)) && (j < 2*(n-1)-i)) ? k-1 : k;
                k = ((k < n) && (j >= 2*(n-1)-i)) ? k+1 : k;
            }
            printf("\n");
        }
        for(int i=n-2; i>-1; i--){
            k = n;
            for(int j=0; j<2*n-1; j++) {
                printf("%d ",k);
                k = (((k-j)>(k-i)) && (j < 2*(n-1)-i)) ? k-1 : k;
                k = ((k < n) && (j >= 2*(n-1)-i)) ? k+1 : k;
            }
    				
    				This code mimics signal processing and stuff used in Digital Systems, Ive used concept of DEMUX and split the loop conditions 
    
        printf("\n");
    }
    return 0;
    

    }