# pattern_10_10.c

Mar 24th, 2022
559
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     pattern_10_10.c
4.
6.
8.
9. n = 4 , number of rows.
10.
11.                   1
12.               10 10
13.         100 100 100
14. 1000 1000 1000 1000
15.
16. In the first row we have those 1 (10 ^ 0)
17. In the second row we have two 10 (10 ^ 1)
18. In the third row we have three 100 (10 ^ 2)
19. . . .
20. And in the nth row we have n 10 ^ n
21.
22. The problem is that the numbers are aligned with the right margin and
23. therefore for each line we have to calculate how many blank characters
24. precede the beginning of the number printing.
25.
26. n = 4 , number of rows.
27.
28. ------------------1     ,  row = 0 has 18 blanks in front of the first digit
29. --------------10 10     ,  row = 1 has 14 blanks in front of the first digit
30. --------100 100 100     ,  row = 2 has  8 blanks in front of the first digit
31. 1000 1000 1000 1000     ,  row = 3 has  0 blanks in front of the first digit
32.
33. The length of the longest row is n*n+n-1 .
34.
35. In each row the numbers take up (row+1)*(row+1)+row places.
36. The rest up to the length of the longest row are blank characters.
37.
38. So, number of blank characters in each row is
39. blanks = n*n+n-1 - ( (row+1)*(row+1)+row ) .
40.
41.
42.     You can find all my C programs at Dragan Milicev's pastebin:
43.
44.     https://pastebin.com/u/dmilicev
45.
46. */
47.
48. #include <stdio.h>
49. #include <math.h>   // due to the function of pow()
50.
51. // implementation of function pow() only for integers
52. int my_pow_for_integers(int base, int exponent){
53.     int result = 1;
54.     for (exponent; exponent>0; exponent--)
55.         result = result * base;
56.     return result;
57. }
58.
59. // print a pattern along the left edge
60. void print_pattern1(int n){
61.     int i, j;
62.
63.     for(i=0;i<n;i++){       // i count rows
64.         for(j=0;j<=i;j++)   // j counts 10^i
65.             printf("%.f ", pow(10,i) );
66.         printf("\n\n");
67.     }
68. }
69.
70. // print a pattern aligned along the right edge
71. void print_pattern2(int n){
72.     int i, row, blanks;
73.
74.     for(row=0;row<n;row++){                 // print rows, row count rows
75.
76.         blanks = n*n+n-1 - ( (row+1)*(row+1)+row ); // calculate how many blanks
77.
78.         for(i=0;i<blanks;i++)               // print blanks, i counts how many
79.             printf("-");
80.
81.         for(i=0;i<=row;i++)                 // print the numbers 10^row, i counts how many
82.             printf("%.f ", pow(10,row) );
83.
84.         printf("\n\n");
85.     }
86. }
87.
88.
89. int main(void){
90.     int n=4;                // n is number of rows
91.
92.     print_pattern1(n);      // print a pattern along the left edge
93.
94.     printf("\n\n");
95.
96.     print_pattern2(n);      // print pattern aligned along the right edge
97.
98. //  printf("\n 10 ^ %d = %d\n", n, my_pow_for_integers(10,n) );
99.
100.     printf("\n\n");
101.     return 0;
102. } // main()
103.