Advertisement
dmilicev

pattern with numbers v1.c

Oct 15th, 2019
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. /*
  2.     pattern with numbers v1.c
  3.  
  4.  Enter a number between 1 and 9 ,    n = 9
  5.  
  6. 1 - - - - - - - - - - - - - - - - 1
  7. 1 2 - - - - - - - - - - - - - - 2 1
  8. 1 2 3 - - - - - - - - - - - - 3 2 1
  9. 1 2 3 4 - - - - - - - - - - 4 3 2 1
  10. 1 2 3 4 5 - - - - - - - - 5 4 3 2 1
  11. 1 2 3 4 5 6 - - - - - - 6 5 4 3 2 1
  12. 1 2 3 4 5 6 7 - - - - 7 6 5 4 3 2 1
  13. 1 2 3 4 5 6 7 8 - - 8 7 6 5 4 3 2 1
  14. 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
  15. 1 2 3 4 5 6 7 8 - - 8 7 6 5 4 3 2 1
  16. 1 2 3 4 5 6 7 - - - - 7 6 5 4 3 2 1
  17. 1 2 3 4 5 6 - - - - - - 6 5 4 3 2 1
  18. 1 2 3 4 5 - - - - - - - - 5 4 3 2 1
  19. 1 2 3 4 - - - - - - - - - - 4 3 2 1
  20. 1 2 3 - - - - - - - - - - - - 3 2 1
  21. 1 2 - - - - - - - - - - - - - - 2 1
  22. 1 - - - - - - - - - - - - - - - - 1
  23.  
  24.  
  25. getch(); is here for you to see how this algorithm works.
  26.  
  27. After that, delete lines with getch();
  28.  
  29. and printf("- "); replace with printf("  ");
  30.  
  31. */
  32.  
  33. #include "stdio.h"
  34.  
  35. int main(void)
  36. {
  37.     int i, j, n, s;
  38.  
  39.     printf("\n Enter a number between 1 and 9 ,  n = ");
  40.     scanf("%d", &n);
  41.  
  42.     printf("\n\n");
  43.  
  44.     for (i = 1; i <= n; i++)        // printing upper half of the pattern, row by row
  45.     {
  46.         for (j = 1; j <= i; j++)        // printing left numbers in row i
  47.         {
  48.             printf("%d ",j);
  49.         }
  50. getch();
  51.         for(s=1; s <= 2 * (n - i); s++) // printing spaces in row i
  52.         {
  53.             printf("- ");               // change with: printf("  ");
  54.         }
  55. getch();
  56.         for (j = i; j >= 1; j--)        // printing right numbers in row i
  57.         {
  58.             printf("%d ",j);
  59.         }
  60.  
  61.         printf("\n");                   // go to next row i
  62.     }
  63.  
  64.  
  65.     for (i = n-1; i >= 1; i--)      // printing lower half of the pattern, row by row
  66.     {
  67.         for (j = 1; j <= i; j++)        // printing numbers
  68.         {
  69.             printf("%d ",j);
  70.         }
  71. getch();
  72.         for(s=1; s <= 2 * (n - i); s++) // printing spaces
  73.         {
  74.             printf("- ");               // change with: printf("  ");
  75.         }
  76. getch();
  77.         for (j = i; j >= 1; j--)        // printing numbers
  78.         {
  79.             printf("%d ",j);
  80.         }
  81.  
  82.         printf("\n");                   // go to next row i
  83.     }
  84.  
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement