Advertisement
dmilicev

char_patterns_v1.c

Oct 18th, 2019
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1. /*
  2.  
  3.     char_patterns_v1.c
  4.  
  5.  
  6.     https://codedost.com/c/number-patterns-c/
  7.  
  8.     https://www.programiz.com/c-programming/examples/pyramid-pattern
  9.  
  10.     https://www.includehelp.com/c-programs/c-program-to-print-stars-in-series-pyramid-using-looping.aspx
  11.  
  12.     https://www.faceprep.in/pattern-programs-in-c/
  13.  
  14.  
  15.     You can find all my C programs at Dragan Milicev's pastebin:
  16.  
  17.     https://pastebin.com/u/dmilicev
  18.  
  19. */
  20.  
  21.  
  22. #include <stdio.h>
  23.  
  24. /*
  25.  
  26. *
  27.  
  28. * *
  29.  
  30. * * *
  31.  
  32. * * * *
  33.  
  34. */
  35. void left_triangle(int number_of_rows)
  36. {
  37.     int row, column;
  38.  
  39.     for(row=0; row<=number_of_rows; row++)
  40.     {
  41.         for(column=0; column<row; column++)
  42.             printf("* ");
  43.  
  44.         printf("\n\n");     // new row
  45.     }
  46. }
  47.  
  48.  
  49. /*
  50.  
  51. * * * *
  52.  
  53. * * *
  54.  
  55. * *
  56.  
  57. *
  58.  
  59. */
  60. void inverted_left_triangle(int number_of_rows)
  61. {
  62.     int i, j, k = 0;
  63.  
  64.     for(i = number_of_rows; i >= 1; --i)
  65.     {
  66.         for(j = 1; j <= i; ++j)
  67.             printf("* ");
  68.  
  69.         printf("\n\n");
  70.     }
  71. }
  72.  
  73.  
  74. /*
  75.  
  76.       *
  77.  
  78.     * *
  79.  
  80.   * * *
  81.  
  82. * * * *
  83.  
  84. */
  85. void right_triangle(int number_of_rows)
  86. {
  87.     int i, j, k;
  88.  
  89.     for(i=0; i<number_of_rows; i++)
  90.     {
  91.         for(j=number_of_rows; j>i+1; j--)
  92.             printf("  ");
  93.  
  94.         for(k=0 ; k<=i; k++)
  95.             printf("* ");
  96.  
  97.         printf("\n\n");
  98.     }
  99. }
  100.  
  101.  
  102. /*
  103.  
  104. * * * *
  105.  
  106.   * * *
  107.  
  108.     * *
  109.  
  110.       *
  111.  
  112. */
  113. void inverted_right_triangle(int number_of_rows)
  114. {
  115.     int i, row, ns, nd;     // ns number of spaces, nd number of digits
  116.  
  117.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  118.     {
  119.         ns = 2*row;                 // calculate ns number of spaces
  120.         nd = number_of_rows-row;    // calculate nd number of digits
  121.  
  122.         for (i=0; i<ns; i++)        // print ns spaces " "
  123.             printf(" ");
  124.  
  125.         for (i=0; i<nd; i++)        // print nd "%d "
  126.             printf("* ");
  127.  
  128.         printf("\n\n");             // print new row
  129.     }
  130. }
  131.  
  132.  
  133. /*
  134.  
  135.    *
  136.  
  137.   * *
  138.  
  139.  * * *
  140.  
  141. * * * *
  142.  
  143. */
  144. void pyramid(int number_of_rows)
  145. {
  146.     int i, row, ns, nd;     // ns number of spaces, nd number of digits
  147.  
  148.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  149.     {
  150.         ns = number_of_rows-row-1;  // calculate ns number of spaces
  151.         nd = row+1;                 // calculate nd number of digits
  152.  
  153.         for (i=0; i<ns; i++)        // print ns spaces " "
  154.             printf(" ");
  155.  
  156.         for (i=0; i<nd; i++)        // print nd "%d "
  157.             printf("* ");
  158.  
  159.         printf("\n\n");             // print new row
  160.     }
  161. }
  162.  
  163.  
  164. /*
  165.  
  166. * * * *
  167.  
  168.  * * *
  169.  
  170.   * *
  171.  
  172.    *
  173.  
  174. */
  175. void inverted_pyramid(int number_of_rows)
  176. {
  177.     int i, row, ns, nd;     // ns number of spaces, nd number of digits
  178.  
  179.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  180.     {
  181.         ns = row;                       // calculate ns number of spaces
  182.         nd = number_of_rows-row;        // calculate nd number of digits
  183.  
  184.         for (i=0; i<ns; i++)            // print ns spaces " "
  185.             printf(" ");
  186.  
  187.         for (i=0; i<nd; i++)            // print nd "%d "
  188.             printf("* ");
  189.  
  190.         printf("\n\n");                 // print new row
  191.     }
  192. }
  193.  
  194.  
  195. /*
  196.  
  197.    *
  198.  
  199.   * *
  200.  
  201.  * * *
  202.  
  203. * * * *
  204.  
  205. * * * *
  206.  
  207.  * * *
  208.  
  209.   * *
  210.  
  211.    *
  212.  
  213. */
  214. void diamond(int number_of_rows)
  215. {
  216.     pyramid(number_of_rows);
  217.     inverted_pyramid(number_of_rows);
  218. }
  219.  
  220.  
  221. int main(void)
  222. {
  223.     int number_of_rows = 5;
  224.  
  225.     printf("\n\t\t left_triangle() \n");
  226.     left_triangle(number_of_rows);
  227.     printf("\n");
  228.  
  229.     printf("\n\t\t inverted_left_triangle() \n\n");
  230.     inverted_left_triangle(number_of_rows);
  231.     printf("\n");
  232.  
  233.     printf("\n\t\t right_triangle() \n\n");
  234.     right_triangle(number_of_rows);
  235.     printf("\n");
  236.  
  237.     printf("\n\t\t inverted_right_triangle() \n\n");
  238.     inverted_right_triangle(number_of_rows);
  239.     printf("\n");
  240.  
  241.     printf("\n\t\t pyramid() \n\n");
  242.     pyramid(number_of_rows);
  243.     printf("\n");
  244.  
  245.     printf("\n\t\t inverted_pyramid() \n\n");
  246.     inverted_pyramid(number_of_rows);
  247.     printf("\n");
  248.  
  249.     printf("\n\t\t diamond \n\n");
  250.     diamond(number_of_rows);
  251.     printf("\n");
  252.  
  253.  
  254.     return(0);
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement