dmilicev

pattern_centre_v1.c

May 6th, 2020
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. /*
  2.  
  3.     pattern_centre_v1.c
  4.  
  5.     Task:
  6.     https://web.facebook.com/groups/c.programing/permalink/1572862292872615/
  7.  
  8.  
  9.     You can find all my C programs at Dragan Milicev's pastebin:
  10.  
  11.     https://pastebin.com/u/dmilicev
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16.  
  17. void print_pattern( int n )
  18. {
  19.     int r, c;           // r is row, c is column
  20.  
  21.     for(c=0; c<n; c++)
  22.     {
  23.         for(r=0; r<n; r++)
  24.         {
  25.             if(n%2)     // if n is odd
  26.             {
  27.                 if( r==n/2 && c==n/2 )
  28.                     printf("1");
  29.                 else
  30.                     printf("2");
  31.             }
  32.             else        // if n is even
  33.             {
  34.                 if( (r==n/2-1 || r==n/2) && (c==n/2-1 || c==n/2) )
  35.                     printf("1");
  36.                 else
  37.                     printf("2");
  38.             }
  39.         } // for(r=0; r<n; r++)
  40.         printf("\n");
  41.     } // for(c=0; c<n; c++)
  42.     printf("\n");
  43. }
  44.  
  45. int main(void)
  46. {
  47.     print_pattern(3);
  48.     print_pattern(4);
  49.     print_pattern(5);
  50.     print_pattern(6);
  51.     print_pattern(7);
  52.     print_pattern(8);
  53.  
  54.     return 0;
  55.  
  56. } // main()
Add Comment
Please, Sign In to add comment