 # five_digits_numbers_divisible_by_12.c

Oct 1st, 2020
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     five_digits_numbers_divisible_by_12.c
4.
6.     How many 5-digit numbers can be formed by using digits 0,1,2,3,4,5
7.     (without repetition) that are divisible by 12?
8.
9. A number is divisible by 12 if the number formed by the last two digits in the number
10. is divisible by 4 AND the sum of the digits in the number is divisible by 3.
11.
12. For example, for 123,456 the last two digits (56) is divisible by 4, AND
13. 1 + 2 + 3 + 4 + 5 + 6 = 21. 21 is divisible by 3.
14. Thus, 123,456 is divisible by 12.
15.
16. However, 123,458 is not divisible by 12 for two reasons:
17. The last two digits (58) is not divisible by 4 and
18. 1 + 2 + 3 + 4 + 5 + 8 = 23 and 23 is not divisible by 3.
19.
20. Remember, both conditions must be met for the number to be divisible by 12.
21.
22. https://divisible.info/Divi.../Divisibility-rule-for-12.html
23.
24.
25. Divisibility rule for 3
26.
27. If the sum of the digits of a number is divisible by 3,
28. then the number is divisible by 3.
29.
30.
31. Divisibility rule for 4
32.
33. If the number formed by the last two digits in a number is divisible by 4,
34. or if the last two digits are 00,
35. the original number is divisible by 4.
36.
37.
38.     You can find all my C programs at Dragan Milicev's pastebin:
39.
40.     https://pastebin.com/u/dmilicev
41.
42. */
43.
44. #include <stdio.h>
45.
46. // returns 1 if all elements are distinct (unique, different from each other)
47. // otherwise returns 0
48. int all_array_elements_are_distinct( int arr[], int size )
49. {
50.     int i, j;
51.
52.     for (i=0; i<size; i++)
53.         for (j=i+1; j<size; j++)
54.             if (arr[i] == arr[j])
55.                 return 0;
56.
57.     return 1;
58. }
59.
60. // If the sum of the digits of a number is divisible by 3, returns 1,
61. // otherwise returns 0
62. int is_divisible_by_3( int d1, int d2, int d3, int d4, int d5 )
63. {
64.     if( (d1 + d2 + d3 + d4 + d5) % 3 == 0 )
65.         return 1;
66.
67.     return 0;
68. }
69.
70. // If the number formed by the last two digits is divisible by 4
71. // or the last two digits are 00, returns 1,
72. // otherwise returns 0
73. int is_divisible_by_4(int d4, int d5)
74. {
75.     int n = d4*10 + d5;
76.
77.     if( n%4==0 || (d4==0 && d5==0) )
78.         return 1;
79.
80.     return 0;
81. }
82.
83.
84. int main(void)
85. {
86.     int d1, d2, d3, d4, d5, counter=0;
87.     int arr_num;
88.
89.     for(d1=1; d1<6; d1++)
90.         for(d2=0; d2<6; d2++)
91.             for(d3=0; d3<6; d3++)
92.                 for(d4=0; d4<6; d4++)
93.                     for(d5=0; d5<6; d5++)
94.                     {
95.                         arr_num = d1;
96.                         arr_num = d2;
97.                         arr_num = d3;
98.                         arr_num = d4;
99.                         arr_num = d5;
100.
101.                         // if all 5 digits are different from each other
102.                         if( all_array_elements_are_distinct(arr_num, 5) )
103.                             if( is_divisible_by_3(d1,d2,d3,d4,d5) )
104.                                 if( is_divisible_by_4(d4,d5) )
105.                                 {
106.                                     counter++;
107.                                     printf(" %d %d %d %d %d \n", d1, d2, d3, d4, d5);
108.                                 }
109.                     }
110.
111.     printf("\n The computer says that there are %d such numbers that are divisible by 12. \n", counter);
112.
113.
114.     return 0;
115.
116. } // main()
117.
RAW Paste Data Copied