Advertisement
dmilicev

square pattern with cross v1.c

Oct 19th, 2019
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. /*
  2.     square pattern with cross v1.c
  3.  
  4.  Program by Debottam Das
  5.  https://www.facebook.com/debottam.das.121
  6.  
  7.  Enter any odd number of rows: 11
  8.  
  9.  output is :
  10.  
  11.           *
  12.         # * #
  13.       # # * # #
  14.     # # # * # # #
  15.   # # # # * # # # #
  16. * * * * * * * * * * *
  17.   # # # # * # # # #
  18.     # # # * # # #
  19.       # # * # #
  20.         # * #
  21.           *
  22.  
  23. */
  24.  
  25.  
  26. #include<stdio.h>
  27.  
  28.  
  29. void square_pattern_with_cross(int n)
  30. {
  31.     int i, j, s, s1=0, s2=0, s3;
  32.  
  33.     s  = n/2;
  34.     s1 = s+1;
  35.     s2 = s;
  36.     s3 = s1;
  37.  
  38.     for(i=1; i<=((n/2)); i++)
  39.     {
  40.         for(j=1; j<=s1; j++)
  41.         {
  42.             if(j<=s && s!=0)
  43.             {
  44.                 printf("  ");
  45.             }
  46.             else if (j == ((n/2)+1))
  47.             {
  48.                 printf("* ");
  49.             }
  50.             else
  51.             {
  52.                 printf("# ");
  53.             }
  54.         }
  55.         s--;
  56.         s1++;
  57.         printf("\n");
  58.     }
  59.  
  60.     for(i=1; i<=n; i++)
  61.     {
  62.         printf("* ");
  63.     }
  64.  
  65.     printf("\n");
  66.  
  67.     s  = 1;
  68.     s1 = (n/2)+(n/2);   // not s1 = n because of integer division !
  69.     s2 = n/2;
  70.  
  71.     for(i=1; i<=s2; i++)
  72.     {
  73.         for(j=1; j<=s1; j++)
  74.         {
  75.             if(j<=s && s!=0)
  76.             {
  77.                 printf("  ");
  78.             }
  79.             else if (j == ((n/2)+1))
  80.             {
  81.                 printf("* ");
  82.             }
  83.             else
  84.             {
  85.                 printf("# ");
  86.             }
  87.         }
  88.  
  89.         s++;
  90.         s1--;
  91.  
  92.         printf("\n");
  93.     }
  94. }
  95.  
  96.  
  97. void main(void)
  98. {
  99.     int number_of_rows;
  100.  
  101.     printf("\n Enter any odd number of rows: ");
  102.     scanf("%d",&number_of_rows);
  103.  
  104.     printf("\n");
  105.  
  106.     if(number_of_rows %2 == 0)
  107.     {
  108.         printf("\n SORRY! With even numbers this pattern is not possible. \n");
  109.         return 1;
  110.     }
  111.  
  112.     square_pattern_with_cross(number_of_rows);
  113.  
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement