Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- number_and_its_square_with_all_different_digits.c
- Task:
- There is a three digit number. Its square is a six digit number.
- Find all three-digit numbers that have all the different digits
- in them and each square.
- For example, 567 is a three digit number. Its square 567*567 = 321489.
- We know that 5, 6, 7 and 3, 2, 1, 4, 8, 9 are all 9 different digits.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- void ShowArray( char text[], int array[], int n )
- {
- int i;
- printf("%s", text);
- for(i=0;i<n;i++)
- printf("%2d", array[i]);
- printf("\n");
- }
- // 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;
- }
- // extract digits from number into array digits[]
- // returns the number of extracted digits
- int extract_digits_from_number( int number, int digits[20] )
- {
- int i, j, number_of_digits = 0, mem;
- while( number != 0 )
- {
- digits[number_of_digits++] = number % 10; // fill array digits[]
- number = number / 10;
- }
- // reverse first number_of_digits elements of array digits[]
- for ( i=0, j=number_of_digits-1; i<number_of_digits/2; i++, j-- )
- {
- mem = digits[i];
- digits[i] = digits[j];
- digits[j] = mem;
- }
- return( number_of_digits );
- } // extract_digits_from_number
- // Concatenates two integer arrays arr1[] and arr2[] into arr3[] and
- // returns number of integers of arr3
- int concatenateTwoArrays(int arr1[], int n1, int arr2[], int n2, int arr3[], int *n3)
- {
- int i;
- *n3 = 0;
- for(i=0;i<n1;i++)
- arr3[(*n3)++] = arr1[i];
- for(i=0;i<n2;i++)
- arr3[(*n3)++] = arr2[i];
- return (*n3);
- }
- // Enter a three-digit number, returns entered num
- int inputThreeDigitsNumber(int *num)
- {
- int ch;
- while(1)
- {
- printf("\n Enter a three-digits number: ");
- scanf("%d", num);
- while ( ( ch = getchar() ) != '\n' ); // empty the input buffer of a stream
- if( *num>99 && *num<1000 )
- return *num;
- else
- printf("\n Entered number is not a three-digits number. Try again. \n");
- }
- }
- int main(void)
- {
- int i, num, count=0;
- int numDigits[3], squareDigits[6], allDigits[9];
- int numLen, squareLen, allLen;
- // Let's test above functions
- inputThreeDigitsNumber(&num);
- printf("\n num = %d \t num^2 = %d \n", num, num*num);
- numLen = extract_digits_from_number(num, numDigits);
- ShowArray("\n numDigits = ",numDigits,numLen);
- squareLen = extract_digits_from_number(num*num, squareDigits);
- ShowArray("\n squareDigits = ",squareDigits,squareLen);
- concatenateTwoArrays(numDigits, numLen, squareDigits, squareLen, allDigits, &allLen); // or
- //allLen = concatenateTwoArrays(numDigits, numLen, squareDigits, squareLen, allDigits, &allLen);
- ShowArray("\n allDigits = ",allDigits,allLen);
- // Let's find such numbers
- for(i=100;i<1000;i++)
- {
- numLen = extract_digits_from_number(i,numDigits);
- squareLen = extract_digits_from_number(i*i, squareDigits);
- concatenateTwoArrays(numDigits, numLen, squareDigits, squareLen, allDigits, &allLen);
- if( all_array_elements_are_distinct(allDigits,allLen) )
- {
- count++;
- printf("\n %2d. \t i = %3d \t i^2 = %6d \n", count, i, i*i );
- }
- }
- printf("\n There are %d such numbers. \n", count );
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment