Advertisement
dmilicev

triangle_from_desired_numbers_v1.c

May 15th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. /*
  2.  
  3.     triangle_from_desired_numbers_v1.c
  4.  
  5.     Task:
  6.     https://web.facebook.com/photo.php?fbid=863006627544618&set=gm.1579899445502233&type=3&theater
  7.  
  8. ---------1
  9. --------212
  10. -------82128
  11. ------6821286
  12. -----068212860
  13. ----20682128602
  14. ---8206821286028
  15. --682068212860286
  16. -06820682128602860
  17. 2068206821286028602
  18.  
  19.     You can find all my C programs at Dragan Milicev's pastebin:
  20.  
  21.     https://pastebin.com/u/dmilicev
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26.  
  27. // Draw n rows of a triangle with numbers
  28. void draw_trinagle_of_numbers( int n )
  29. {
  30.     int num[4]={2,8,6,0};               // array of desired digits
  31.     int i,row, index=0, ns, nd;         // ns number of spaces, nd number of digits
  32.                                         // index is for array num[index] that we print
  33.     for (row=0; row<n; row++)           // print rows one by one row
  34.     {
  35.         ns = n-row-1;                   // calculate ns number of spaces
  36.         nd = (2*row+1)/2;               // calculate nd number of digits
  37.  
  38.         for (i=0; i<ns; i++)            // print ns spaces " "
  39.             printf("-");
  40.  
  41.         for(i=0; i<nd; i++)             // print nd "%d " numbers
  42.         {
  43.             printf("%d", num[index] );
  44.             if(index>0)
  45.                 index--;
  46.             else
  47.                 index=3;
  48.         }
  49.  
  50.         printf("%d", 1 );               // printf digit 1 in middle
  51.  
  52.         index = 0;
  53.         for(i=0; i<nd; i++)             // print nd "%d " numbers
  54.         {
  55.             printf("%d", num[index] );
  56.             if(index<3)
  57.                 index++;
  58.             else
  59.                 index=0;
  60.         }
  61.  
  62.         printf("\n");
  63.     }
  64. }
  65.  
  66.  
  67. int main(void)
  68. {
  69.     draw_trinagle_of_numbers( 10 );
  70.  
  71.     return 0;
  72.  
  73. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement