Advertisement
dmilicev

diamond star wall pattern with abs() and longest row v1.c

Oct 15th, 2019
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. /*
  2.  diamond star wall pattern with abs() and longest row v1.c
  3.  
  4.  Enter the number of stars in the longest row of diamond, ns.
  5.  
  6.  The number of stars in the longest row of diamond is same as
  7.  the number of rows of the upper triangle of diamond.
  8.  
  9.  Diamond will have 2*ns-1 raws.
  10.  
  11.  We enter maximum number of stars in single row,
  12.  then we draw such a diamond.
  13.  
  14. ns = 5
  15.  
  16. ----*-
  17. ---*-*-
  18. --*-*-*-
  19. -*-*-*-*-
  20. *-*-*-*-*-
  21. -*-*-*-*-
  22. --*-*-*-
  23. ---*-*-
  24. ----*-
  25.  
  26. row  spaces,j   *          i
  27.  
  28.  0.     4       1         -4
  29.  1.     3       2         -3
  30.  2.     2       3         -2
  31.  3.     1       4         -1
  32.  4.     0       5          0
  33.  5.     1       4          1
  34.  6.     2       3          2
  35.  7.     3       2          3
  36.  8.     4       1          4
  37.  
  38. */
  39.  
  40. #include "stdio.h"
  41.  
  42. int main(void)
  43. {
  44. /*
  45.     i   counter in loop for rows
  46.     j   counter in print loops
  47.     ns  number of rows of the upper triangle of diamond
  48.         or number of stars in the longest row of diamond
  49. */
  50.     int i, j, ns;
  51.  
  52.     printf("\n the number of stars in the longest row of diamond, ns = ");
  53.     scanf("%d", &ns);                       // try 5, 6 or 30
  54.  
  55.     printf("\n");
  56.  
  57.     for (i = -ns + 1; i < ns; i++)          // loop for rows
  58.     {
  59. //      printf("%*s", abs(i), "");          // this line or for (j = 0; j < abs(i); j++)
  60.  
  61.         for (j = 0; j < abs(i); j++)        // print "-"
  62.             printf("-");
  63.  
  64.         for (j = 0; j < ns - abs(i); j++)   // print "*-"
  65.             printf("*-");
  66.  
  67.         printf("\n");                       // new row
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement