dmilicev

number_and_its_square_with_all_different_digits.c

Sep 28th, 2020
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     number_and_its_square_with_all_different_digits.c
  4.  
  5.     Task:
  6.     There is a three digit number. Its square is a six digit number.
  7.     Find all three-digit numbers that have all the different digits
  8.     in them and each square.
  9.     For example, 567 is a three digit number. Its square 567*567 = 321489.
  10.     We know that 5, 6, 7 and 3, 2, 1, 4, 8, 9 are all 9 different digits.
  11.  
  12.  
  13.     You can find all my C programs at Dragan Milicev's pastebin:
  14.  
  15.     https://pastebin.com/u/dmilicev
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. void ShowArray( char text[], int array[], int n )
  22. {
  23.     int i;
  24.  
  25.     printf("%s", text);
  26.  
  27.     for(i=0;i<n;i++)
  28.         printf("%2d", array[i]);
  29.  
  30.     printf("\n");
  31. }
  32.  
  33. // returns 1 if all elements are distinct (unique, different from each other)
  34. // otherwise returns 0
  35. int all_array_elements_are_distinct( int arr[], int size )
  36. {
  37.     int i, j;
  38.  
  39.     for (i=0; i<size; i++)
  40.         for (j=i+1; j<size; j++)
  41.             if (arr[i] == arr[j])
  42.                 return 0;
  43.  
  44.     return 1;
  45. }
  46.  
  47. // extract digits from number into array digits[]
  48. // returns the number of extracted digits
  49. int extract_digits_from_number( int number, int digits[20] )
  50. {
  51.     int i, j, number_of_digits = 0, mem;
  52.  
  53.     while( number != 0 )
  54.     {
  55.         digits[number_of_digits++] = number % 10;   // fill array digits[]
  56.         number = number / 10;
  57.     }
  58.  
  59.     // reverse first number_of_digits elements of array digits[]
  60.     for ( i=0, j=number_of_digits-1; i<number_of_digits/2; i++, j-- )
  61.     {
  62.         mem = digits[i];
  63.         digits[i] = digits[j];
  64.         digits[j] = mem;
  65.     }
  66.  
  67.     return( number_of_digits );
  68. } // extract_digits_from_number
  69.  
  70. // Concatenates two integer arrays arr1[] and arr2[] into arr3[] and
  71. // returns number of integers of arr3
  72. int concatenateTwoArrays(int arr1[], int n1, int arr2[], int n2, int arr3[], int *n3)
  73. {
  74.     int i;
  75.  
  76.     *n3 = 0;
  77.  
  78.     for(i=0;i<n1;i++)
  79.         arr3[(*n3)++] = arr1[i];
  80.  
  81.     for(i=0;i<n2;i++)
  82.         arr3[(*n3)++] = arr2[i];
  83.  
  84.  
  85.     return (*n3);
  86. }
  87.  
  88. // Enter a three-digit number, returns entered num
  89. int inputThreeDigitsNumber(int *num)
  90. {
  91.     int ch;
  92.  
  93.     while(1)
  94.     {
  95.         printf("\n Enter a three-digits number: ");
  96.         scanf("%d", num);
  97.         while ( ( ch = getchar() ) != '\n' );   // empty the input buffer of a stream
  98.  
  99.         if( *num>99 && *num<1000 )
  100.             return *num;
  101.         else
  102.             printf("\n Entered number is not a three-digits number. Try again. \n");
  103.     }
  104. }
  105.  
  106.  
  107. int main(void)
  108. {
  109.     int i, num, count=0;
  110.     int numDigits[3], squareDigits[6], allDigits[9];
  111.     int numLen, squareLen, allLen;
  112.  
  113.     // Let's test above functions
  114.     inputThreeDigitsNumber(&num);
  115.     printf("\n num = %d \t num^2 = %d \n", num, num*num);
  116.  
  117.     numLen = extract_digits_from_number(num, numDigits);
  118.     ShowArray("\n numDigits =   ",numDigits,numLen);
  119.  
  120.     squareLen = extract_digits_from_number(num*num, squareDigits);
  121.     ShowArray("\n squareDigits =      ",squareDigits,squareLen);
  122.  
  123.     concatenateTwoArrays(numDigits, numLen, squareDigits, squareLen, allDigits, &allLen);   // or
  124.     //allLen = concatenateTwoArrays(numDigits, numLen, squareDigits, squareLen, allDigits, &allLen);
  125.     ShowArray("\n allDigits =   ",allDigits,allLen);
  126.  
  127.     // Let's find such numbers
  128.     for(i=100;i<1000;i++)
  129.     {
  130.         numLen = extract_digits_from_number(i,numDigits);
  131.         squareLen = extract_digits_from_number(i*i, squareDigits);
  132.         concatenateTwoArrays(numDigits, numLen, squareDigits, squareLen, allDigits, &allLen);
  133.  
  134.         if( all_array_elements_are_distinct(allDigits,allLen) )
  135.         {
  136.             count++;
  137.             printf("\n %2d. \t i = %3d \t i^2 = %6d \n", count, i, i*i );
  138.         }
  139.     }
  140.  
  141.     printf("\n There are %d such numbers. \n", count );
  142.  
  143.     return 0;
  144.  
  145. } // main()
  146.  
RAW Paste Data Copied