Advertisement
dmilicev

number_pattern_pyramid.c

Feb 16th, 2021
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. /*
  2.  
  3.     number_pattern_pyramid.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>                 // for abs()
  15.  
  16. /*
  17.  
  18. - - - *-
  19.  
  20. - - *-*-*-
  21.  
  22. - *-*-*-*-*-
  23.  
  24. *-*-*-*-*-*-*-
  25.  
  26. row     ns      nc      n = 4
  27.  0       3       1
  28.  1       2       3
  29.  2       1       5
  30.  3       0       7
  31.  
  32.  ns = n-row-1;
  33.  nc = 2*row+1;
  34.  
  35. */
  36. void pyramid_of_char_v1(int n)
  37. {
  38.     int i, row, ns, nc;             // ns number of spaces, nc number of chars
  39.  
  40.     for (row=0; row<n; row++)       // print rows one by one row
  41.     {
  42.         ns = n-row-1;               // calculate ns number of spaces
  43.         nc = 2*row+1;               // calculate nc number of chars
  44.  
  45.         for (i=0; i<ns; i++)        // print ns spaces " "
  46.             printf("- ");
  47.  
  48.         for (i=0; i<nc; i++)        // print nc "%d "
  49.             printf("*-");
  50.  
  51.         printf("\n\n");             // print new row
  52.     }
  53. }
  54.  
  55. /*
  56.  
  57. - - - 1-
  58.  
  59. - - 2-1-2-
  60.  
  61. - 3-2-1-2-3-
  62.  
  63. 4-3-2-1-2-3-4-
  64.  
  65. */
  66. void pyramid_of_numbers_v1(int n)
  67. {
  68.     int i, row, ns, nc, num;        // ns number of spaces, nc number of chars
  69.  
  70.     for (row=0; row<n; row++)       // print rows one by one row
  71.     {
  72.         ns = n-row-1;               // calculate ns number of spaces
  73.         nc = 2*row+1;               // calculate nc number of chars
  74.         num = -(row+2);             // calculate number
  75.  
  76.         for (i=0; i<ns; i++)        // print ns spaces " "
  77.             printf("- ");
  78.  
  79.         for (i=0; i<nc; i++)        // print nc "%d "
  80.         {
  81.             num++;
  82.             if(num==0)
  83.                 num=2;
  84.  
  85.             printf("%d-", abs(num) );
  86.         }
  87.  
  88.         printf("\n\n");             // print new row
  89.     }
  90. }
  91.  
  92. /*
  93.  
  94. ---*
  95. --***
  96. -*****
  97. *******
  98.  
  99. row     ns      nc      n = 4
  100.  0       3       1
  101.  1       2       3
  102.  2       1       5
  103.  3       0       7
  104.  
  105.  ns = n-row-1;
  106.  nc = 2*row+1;
  107.  
  108. */
  109. void pyramid_of_char_v2(int n)
  110. {
  111.     int i, row, ns, nc;             // ns number of spaces, nc number of chars
  112.  
  113.     for (row=0; row<n; row++)       // print rows one by one row
  114.     {
  115.         ns = n-row-1;               // calculate ns number of spaces
  116.         nc = 2*row+1;               // calculate nc number of chars
  117.  
  118.         for (i=0; i<ns; i++)        // print ns spaces " "
  119.             printf("-");
  120.  
  121.         for (i=0; i<nc; i++)        // print nc "*"
  122.             printf("*");
  123.  
  124.         printf("\n\n");             // print new row
  125.     }
  126. }
  127.  
  128. /*
  129.  
  130.    1
  131.   212
  132.  32123
  133. 4321234
  134.  
  135. */
  136. void pyramid_of_numbers_v2(int n)
  137. {
  138.     int i, row, ns, nc, num;        // ns number of spaces, nc number of chars
  139.  
  140.     for (row=0; row<n; row++)       // print rows one by one row
  141.     {
  142.         ns = n-row-1;               // calculate ns number of spaces
  143.         nc = 2*row+1;               // calculate nc number of chars
  144.         num = -(row+2);             // calculate number
  145.  
  146.         for (i=0; i<ns; i++)        // print ns spaces " "
  147.             printf("-");
  148.  
  149.         for (i=0; i<nc; i++)        // print nc "%d"
  150.         {
  151.             num++;
  152.             if(num==0)
  153.                 num=2;
  154.  
  155.             printf("%d", abs(num) );
  156.         }
  157.  
  158.         printf("\n\n");             // print new row
  159.     }
  160. }
  161.  
  162. int main(void)
  163. {
  164.     int n = 5;                      // number of rows
  165.  
  166.     printf("\n\t\t pyramid_of_char_v1() \n\n");
  167.     pyramid_of_char_v1(n);
  168.     printf("\n");
  169.  
  170.     printf("\n\t\t pyramid_of_numbers_v1() \n\n");
  171.     pyramid_of_numbers_v1(n);
  172.     printf("\n");
  173.  
  174.  
  175.     printf("\n\t\t pyramid_of_char_v2() \n\n");
  176.     pyramid_of_char_v2(n);
  177.     printf("\n");
  178.  
  179.     printf("\n\t\t pyramid_of_numbers_v2() \n\n");
  180.     pyramid_of_numbers_v2(n);
  181.     printf("\n");
  182.  
  183.     return(0);
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement