Advertisement
dmilicev

number patterns v1.c

Oct 18th, 2019
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. /*
  2.  
  3.     number pattern v1.c
  4.  
  5.     https://codedost.com/c/number-patterns-c/
  6.  
  7.     https://www.programiz.com/c-programming/examples/pyramid-pattern
  8.  
  9.     https://www.includehelp.com/c-programs/c-program-to-print-stars-in-series-pyramid-using-looping.aspx
  10.  
  11.     https://www.faceprep.in/pattern-programs-in-c/
  12.  
  13.  
  14.     You can find all my C programs at Dragan Milicev's pastebin:
  15.  
  16.     https://pastebin.com/u/dmilicev
  17.  
  18.     https://www.facebook.com/dmilicev
  19.  
  20. */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>     // for abs()
  25.  
  26.  
  27.  
  28. /*
  29.  
  30. 1-
  31.  
  32. 2-3-
  33.  
  34. 4-5-6-
  35.  
  36. */
  37. void left_triangle(int number_of_rows)
  38. {
  39.     int row, column, number=1;
  40.  
  41.     printf("\n\t\t left_triangle() \n");
  42.  
  43.     for(row=0; row<=number_of_rows; row++)
  44.     {
  45.         for(column=0; column<row; column++)
  46.         {
  47.             printf("%d-", number);
  48.             number++;
  49.         }
  50.  
  51.         printf("\n\n"); // new row
  52.     }
  53. }
  54.  
  55.  
  56. /*
  57.  
  58. 1-2-3-
  59.  
  60. 4-5-
  61.  
  62. 6-
  63.  
  64. */
  65. void inverted_left_triangle(int number_of_rows)
  66. {
  67.     int i, j, k = 0, number=1;
  68.  
  69.     printf("\n\t\t inverted_left_triangle() \n\n");
  70.  
  71.     for(i = number_of_rows; i >= 1; --i)
  72.     {
  73.         for(j = 1; j <= i; ++j)
  74.         {
  75.             printf("%d-", number);
  76.             number++;
  77.         }
  78.  
  79.         printf("\n\n");
  80.     }
  81. }
  82.  
  83.  
  84. /*
  85. ----1-
  86.  
  87. --2-3-
  88.  
  89. 4-5-6-
  90.  
  91. */
  92. void right_triangle(int number_of_rows)
  93. {
  94.     int i, j, number=1;
  95.  
  96.     printf("\n\t\t right_triangle() \n\n");
  97.  
  98.     for(i=0 ; i<number_of_rows ; i++)           // print rows
  99.     {
  100.         for(j=2*number_of_rows-i-2 ; j>i ; j--) // print spaces " "
  101.             printf("-");
  102.  
  103.         for(j=0 ; j<=i; j++)                    // print numbers
  104.         {
  105.             printf("%d-", number);
  106.             number++;
  107.         }
  108.         printf("\n\n");
  109.     }
  110. }
  111.  
  112.  
  113. /*
  114.  
  115. 1-2-3-
  116.  
  117. --4-5-
  118.  
  119. ----6-
  120.  
  121. */
  122. void inverted_right_triangle(int number_of_rows)
  123. {
  124.     int i, row, number=1, ns, nd;   // ns number of spaces, nd number of digits
  125.  
  126.     printf("\n\t\t inverted_right_triangle() \n\n");
  127.  
  128.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  129.     {
  130.         ns = 2*row;                 // calculate ns number of spaces
  131.         nd = number_of_rows-row;    // calculate nd number of digits
  132.  
  133.         for (i=0; i<ns; i++)        // print ns spaces " "
  134.             printf("-");
  135.  
  136.         for (i=0; i<nd; i++)        // print nd "%d "
  137.         {
  138.             printf("%d-", number);
  139.             number++;
  140.         }
  141.  
  142.         printf("\n\n");             // print new row
  143.     }
  144. }
  145.  
  146.  
  147. /*
  148.  
  149. --1-
  150.  
  151. -2-3-
  152.  
  153. 4-5-6-
  154.  
  155. */
  156. void pyramid(int number_of_rows)
  157. {
  158.     int i, row, number=1, ns, nd;   // ns number of spaces, nd number of digits
  159.  
  160.     printf("\n\t\t pyramid() \n\n");
  161.  
  162.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  163.     {
  164.         ns = number_of_rows-row-1;  // calculate ns number of spaces
  165.         nd = row+1;                 // calculate nd number of digits
  166.  
  167.         for (i=0; i<ns; i++)        // print ns spaces " "
  168.             printf("-");
  169.  
  170.         for (i=0; i<nd; i++)        // print nd "%d "
  171.         {
  172.             printf("%d-", number);
  173.             number++;
  174.         }
  175.  
  176.         printf("\n\n");             // print new row
  177.     }
  178. }
  179.  
  180.  
  181. /*
  182.  
  183. 1-2-3-
  184.  
  185. -4-5-
  186.  
  187. --6-
  188.  
  189. */
  190. void inverted_pyramid(int number_of_rows)
  191. {
  192.     int i, row, number=1, ns, nd;   // ns number of spaces, nd number of digits
  193.  
  194.     printf("\n\t\t inverted_pyramid() \n\n");
  195.  
  196.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  197.     {
  198.         ns = row;   // calculate ns number of spaces
  199.         nd = number_of_rows-row;                    // calculate nd number of digits
  200.  
  201.         for (i=0; i<ns; i++)        // print ns spaces " "
  202.             printf("-");
  203.  
  204.         for (i=0; i<nd; i++)        // print nd "%d "
  205.         {
  206.             printf("%d-", number);
  207.             number++;
  208.         }
  209.  
  210.         printf("\n\n");             // print new row
  211.     }
  212. }
  213.  
  214.  
  215. /*
  216.  
  217. 1-2-3-4-5-6-7-8-9-8-7-6-5-4-3-2-1-
  218. 1-2-3-4-5-6-7-8---8-7-6-5-4-3-2-1-
  219. 1-2-3-4-5-6-7-------7-6-5-4-3-2-1-
  220. 1-2-3-4-5-6-----------6-5-4-3-2-1-
  221. 1-2-3-4-5---------------5-4-3-2-1-
  222. 1-2-3-4-------------------4-3-2-1-
  223. 1-2-3-----------------------3-2-1-
  224. 1-2---------------------------2-1-
  225. 1-------------------------------1-
  226.  
  227. */
  228. void number_pattern_2(int n)
  229. {
  230.     int i, j, k, start;
  231.  
  232.     printf("\n\t\t number_pattern_2() \n\n");
  233.  
  234.     k = n;
  235.  
  236.     for(i=0; i<n; i++)
  237.     {
  238.         start = 0;
  239.  
  240.         for(j=0; j<2*n-1; j++)
  241.         {
  242.             if(j < n)
  243.                 start++;
  244.             else
  245.                 start--;
  246.  
  247.             if(j<k || j>2*n-k-2)
  248.                 printf("%d-",start);
  249.             else
  250.                 printf("--");
  251.         }
  252.  
  253.         k--;
  254.         printf("\n");
  255.     }
  256. }
  257.  
  258.  
  259. /*
  260. --------1-
  261.  
  262. -------2-2-
  263.  
  264. ------3-3-3-
  265.  
  266. -----4-4-4-4-
  267.  
  268. ----5-5-5-5-5-
  269.  
  270. ---6-6-6-6-6-6-
  271.  
  272. --7-7-7-7-7-7-7-
  273.  
  274. -8-8-8-8-8-8-8-8-
  275.  
  276. 9-9-9-9-9-9-9-9-9-
  277. */
  278. void triangle_of_numbers_1( int number_of_rows )
  279. {
  280.     int i, row, ns, nd;             // ns number of spaces, nd number of digits
  281.  
  282.     printf("\n\t\t triangle_of_numbers_1() \n\n");
  283.  
  284.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  285.     {
  286.         ns = number_of_rows-row-1;  // calculate ns number of spaces
  287.         nd = row+1;                 // calculate nd number of digits
  288.  
  289.         for (i=0; i<ns; i++)        // print ns spaces " "
  290.             printf("-");
  291.  
  292.         for (i=0; i<nd; i++)        // print nd "%d "
  293.             printf("%d-", row+1);
  294.  
  295.         printf("\n\n");             // print new row
  296.     }
  297. }
  298.  
  299.  
  300. /*
  301. --------1-
  302.  
  303. -------1-2-
  304.  
  305. ------1-2-3-
  306.  
  307. -----1-2-3-4-
  308.  
  309. ----1-2-3-4-5-
  310.  
  311. ---1-2-3-4-5-6-
  312.  
  313. --1-2-3-4-5-6-7-
  314.  
  315. -1-2-3-4-5-6-7-8-
  316.  
  317. 1-2-3-4-5-6-7-8-9-
  318. */
  319. void triangle_of_numbers_2( int number_of_rows )
  320. {
  321.     int i, row, number, ns, nd;     // ns number of spaces, nd number of digits
  322.  
  323.     printf("\n\t\t pyramid() \n\n");
  324.  
  325.     for (row=0; row<number_of_rows; row++)  // print rows one by one row
  326.     {
  327.         ns = number_of_rows-row-1;  // calculate ns number of spaces
  328.         nd = row+1;                 // calculate nd number of digits
  329.  
  330.         for (i=0; i<ns; i++)        // print ns spaces " "
  331.             printf("-");
  332.  
  333.         number = 1;
  334.         for (i=0; i<nd; i++)        // print nd "%d "
  335.         {
  336.             printf("%d-", number);
  337.             number++;
  338.         }
  339.  
  340.         printf("\n\n");             // print new row
  341.     }
  342. }
  343.  
  344.  
  345. /*
  346.  
  347. ----------------1-
  348. --------------1-2-1-
  349. ------------1-2-3-2-1-
  350. ----------1-2-3-4-3-2-1-
  351. --------1-2-3-4-5-4-3-2-1-
  352. ------1-2-3-4-5-6-5-4-3-2-1-
  353. ----1-2-3-4-5-6-7-6-5-4-3-2-1-
  354. --1-2-3-4-5-6-7-8-7-6-5-4-3-2-1-
  355. 1-2-3-4-5-6-7-8-9-8-7-6-5-4-3-2-1-
  356.  
  357. */
  358. void pyramid_pattern_1( int n )     // n is number of rows
  359. {
  360.     int i,row, number, ns, nd;      // ns number of spaces, nd number of digits
  361.  
  362.     printf("\n\t\t pyramid_pattern_1() \n\n");
  363.  
  364.     for (row=0; row<n; row++)       // print rows one by one row
  365.     {
  366.         ns = 2*(n-row-1);           // calculate ns number of spaces
  367.         nd = 2*row+1;               // calculate nd number of digits
  368.  
  369.         for (i=0; i<ns; i++)        // print ns spaces " "
  370.             printf("-");
  371.  
  372.         number = 0;
  373.  
  374.         for(i=0; i<nd; i++)         // print nd "%d " numbers
  375.         {
  376.             if( i <= (2*row+1)/2 )
  377.                 number++;
  378.             else
  379.                 number--;
  380.  
  381.             printf("%d-", number );
  382.         }
  383.         printf("\n");
  384.     }
  385. }
  386.  
  387.  
  388. /*
  389.  
  390. ----------------9-
  391. --------------8-9-8-
  392. ------------7-8-9-8-7-
  393. ----------6-7-8-9-8-7-6-
  394. --------5-6-7-8-9-8-7-6-5-
  395. ------4-5-6-7-8-9-8-7-6-5-4-
  396. ----3-4-5-6-7-8-9-8-7-6-5-4-3-
  397. --2-3-4-5-6-7-8-9-8-7-6-5-4-3-2-
  398. 1-2-3-4-5-6-7-8-9-8-7-6-5-4-3-2-1-
  399.  
  400. */
  401. void pyramid_pattern_2( int n )     // n is number of rows
  402. {
  403.     int i, row, number, middle_number, ns, nd;  // ns number of spaces, nd number of digits
  404.  
  405.     printf("\n\t\t pyramid_pattern_2() \n\n");
  406.  
  407.     for (row=0; row<n; row++)       // print rows one by one row
  408.     {
  409.         ns = 2*(n-row-1);           // calculate ns number of spaces
  410.         nd = 2*row+1;               // calculate nd number of digits
  411.  
  412.         for (i=0; i<ns; i++)        // print ns spaces " "
  413.             printf("-");
  414.  
  415.         middle_number = n;
  416.         number = middle_number - row - 1;
  417.  
  418.         for(i=0; i<nd; i++)         // print nd "%d " numbers
  419.         {
  420.             if( i <= (2*row+1)/2 )
  421.                 number++;
  422.             else
  423.                 number--;
  424.  
  425.             printf("%d-", number );
  426.         }
  427.         printf("\n");
  428.     }
  429. }
  430.  
  431.  
  432. /*
  433.  
  434. ------3
  435.  
  436. ----3 2
  437.  
  438. --3 2 1
  439.  
  440. 3 2 1 0
  441.  
  442. --3 2 1
  443.  
  444. ----3 2
  445.  
  446. ------3
  447.  
  448. */
  449. void left_triangle_of_numbers_1( int n )    // n is bigest number
  450. {
  451.     int i, j, k;
  452.  
  453.     printf("\n\t\t left_triangle_of_numbers_1() \n\n");
  454.  
  455.     for( i=n; i>=-n; i-- )                  // print rows
  456.     {
  457.         for( j=1; j<=abs(i); j++ )          // print spaces
  458.             printf("--");
  459.  
  460.         for( k=n; k>=abs(i); k-- )          // print numbers
  461.             printf("%d ", k );
  462.  
  463.         printf("\n\n");                     // new row
  464.     }
  465. }
  466.  
  467.  
  468. /*
  469.  
  470. ---3
  471. --32
  472. -321
  473. 3210
  474. -321
  475. --32
  476. ---3
  477.  
  478. */
  479. void left_triangle_of_numbers_2( int n )    // n is bigest number
  480. {
  481.     int i, j, k;
  482.  
  483.     printf("\n\t\t left_triangle_of_numbers_2() \n\n");
  484.  
  485.     for( i=n; i>=-n; i-- )                  // print rows
  486.     {
  487.         for( j=1; j<=abs(i); j++ )          // print spaces
  488.             printf("-");
  489.  
  490.         for( k=n; k>=abs(i); k-- )          // print numbers
  491.             printf("%d", k );
  492.  
  493.         printf("\n");                       // new row
  494.     }
  495. }
  496.  
  497.  
  498.  
  499. int main(void)
  500. {
  501.     int number_of_rows = 3;
  502.  
  503.     left_triangle(number_of_rows);
  504.     printf("\n");
  505.  
  506.     inverted_left_triangle(number_of_rows);
  507.     printf("\n");
  508.  
  509.     right_triangle(number_of_rows);
  510.     printf("\n");
  511.  
  512.     inverted_right_triangle(number_of_rows);
  513.     printf("\n");
  514.  
  515.     pyramid(number_of_rows);
  516.     printf("\n");
  517.  
  518.     inverted_pyramid(number_of_rows);
  519.     printf("\n");
  520.  
  521.     number_pattern_2(9);
  522.     printf("\n");
  523.  
  524.     triangle_of_numbers_1(9);
  525.     printf("\n");
  526.  
  527.     triangle_of_numbers_2(9);
  528.     printf("\n");
  529.  
  530.     pyramid_pattern_1(9);
  531.     printf("\n");
  532.  
  533.     pyramid_pattern_2(9);
  534.     printf("\n");
  535.  
  536.     left_triangle_of_numbers_1(number_of_rows);
  537.     printf("\n");
  538.  
  539.     left_triangle_of_numbers_2(number_of_rows);
  540.     printf("\n");
  541.  
  542.  
  543.     return(0);
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement