Advertisement

ones_tens_hundreds.c

Dec 15th, 2019
182
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     ones_tens_hundreds.c        by Dragan Milicev
4.
5.
6.     Find all positive three-digit integers whose digits meet the requirement
7.
8.     hundreds + tens + ones = hundreds * tens * ones
9.
10.
11.
12.     You can find all my C programs at Dragan Milicev's pastebin:
13.
14.     https://pastebin.com/u/dmilicev
15.
16. */
17.
18. #include <stdio.h>
19.
20. // extract digits from number into array digits[]
21. // returns the number of extracted digits
22. int extract_digits_from_number( int number, int digits[20] )
23. {
24.     int i, j, number_of_digits = 0, mem;
25.
26.     while( number != 0 )
27.     {
28.         digits[number_of_digits++] = number % 10;   // fill array digits[]
29.         number = number / 10;
30.     }
31.
32.     // reverse first number_of_digits elements of array digits[]
33.     for ( i=0, j=number_of_digits-1; i<number_of_digits/2; i++, j-- )
34.     {
35.         mem = digits[i];
36.         digits[i] = digits[j];
37.         digits[j] = mem;
38.     }
39.
40.     return( number_of_digits );
41.
42. } // extract_digits_from_number
43.
44.
45. int main(void)
46. {
47.     int digits[20];         // array to store digits of number
48.     int i, counter=0;
49.
50.     for ( i=100; i<1000; i++)
51.     {
52.         extract_digits_from_number( i, digits );
53.
54.         if ( digits[0]+digits[1]+digits[2] == digits[0]*digits[1]*digits[2] )
55.         {
56.             counter++;
57.             printf("\n %4d. \t %5d \n", counter, i );
58.         }
59.     }
60.
61.     printf("\n\n From 100 to 999, there are %d such numbers. \n\n", counter );
62.
63.
64.     return 0;
65.
66. } // main()
Advertisement
RAW Paste Data Copied
Advertisement