Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- five_digits_numbers_divisible_by_12.c
- Task:
- How many 5-digit numbers can be formed by using digits 0,1,2,3,4,5
- (without repetition) that are divisible by 12?
- A number is divisible by 12 if the number formed by the last two digits in the number
- is divisible by 4 AND the sum of the digits in the number is divisible by 3.
- For example, for 123,456 the last two digits (56) is divisible by 4, AND
- 1 + 2 + 3 + 4 + 5 + 6 = 21. 21 is divisible by 3.
- Thus, 123,456 is divisible by 12.
- However, 123,458 is not divisible by 12 for two reasons:
- The last two digits (58) is not divisible by 4 and
- 1 + 2 + 3 + 4 + 5 + 8 = 23 and 23 is not divisible by 3.
- Remember, both conditions must be met for the number to be divisible by 12.
- https://divisible.info/Divi.../Divisibility-rule-for-12.html
- Divisibility rule for 3
- If the sum of the digits of a number is divisible by 3,
- then the number is divisible by 3.
- Divisibility rule for 4
- If the number formed by the last two digits in a number is divisible by 4,
- or if the last two digits are 00,
- the original number is divisible by 4.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // returns 1 if all elements are distinct (unique, different from each other)
- // otherwise returns 0
- int all_array_elements_are_distinct( int arr[], int size )
- {
- int i, j;
- for (i=0; i<size; i++)
- for (j=i+1; j<size; j++)
- if (arr[i] == arr[j])
- return 0;
- return 1;
- }
- // If the sum of the digits of a number is divisible by 3, returns 1,
- // otherwise returns 0
- int is_divisible_by_3( int d1, int d2, int d3, int d4, int d5 )
- {
- if( (d1 + d2 + d3 + d4 + d5) % 3 == 0 )
- return 1;
- return 0;
- }
- // If the number formed by the last two digits is divisible by 4
- // or the last two digits are 00, returns 1,
- // otherwise returns 0
- int is_divisible_by_4(int d4, int d5)
- {
- int n = d4*10 + d5;
- if( n%4==0 || (d4==0 && d5==0) )
- return 1;
- return 0;
- }
- int main(void)
- {
- int d1, d2, d3, d4, d5, counter=0;
- int arr_num[5];
- for(d1=1; d1<6; d1++)
- for(d2=0; d2<6; d2++)
- for(d3=0; d3<6; d3++)
- for(d4=0; d4<6; d4++)
- for(d5=0; d5<6; d5++)
- {
- arr_num[0] = d1;
- arr_num[1] = d2;
- arr_num[2] = d3;
- arr_num[3] = d4;
- arr_num[4] = d5;
- // if all 5 digits are different from each other
- if( all_array_elements_are_distinct(arr_num, 5) )
- if( is_divisible_by_3(d1,d2,d3,d4,d5) )
- if( is_divisible_by_4(d4,d5) )
- {
- counter++;
- printf(" %d %d %d %d %d \n", d1, d2, d3, d4, d5);
- }
- }
- printf("\n The computer says that there are %d such numbers that are divisible by 12. \n", counter);
- return 0;
- } // main()
RAW Paste Data
Copied