Advertisement
Guest User

Get unique digits from array

a guest
Dec 11th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int a[]= {2,8,121,42,222,23};
  7.     int b[100] = {}; ///array to store unique digits and check them with tmp
  8.     int unique_digit_products = 0;
  9.     int len;
  10.     int len_b = 1; ///length of array with unique digits
  11.     int i,j;
  12.     int tmp;
  13.     int multiplication = 1;
  14.     int reminder;
  15.     int unique_digits = 0;
  16.  
  17.     ///set len to length of array (count from 0)
  18.     len = 6;
  19.  
  20.     for(i=0; i < len; i++)
  21.     {
  22.         tmp = a[i]; ///get value of array at i index
  23.         while(tmp !=0)
  24.         {
  25.             reminder = tmp % 10;
  26.             multiplication *= reminder;
  27.             tmp /= 10;
  28.         }
  29.  
  30.         for(j=0; j < len_b; j++)
  31.         {
  32.             if(multiplication == b[j])
  33.             {
  34.                 break;
  35.             }
  36.             else
  37.             {
  38.                 if(j == len_b-1)
  39.                 {
  40.                     b[len_b-1] = multiplication;
  41.                     len_b++;
  42.                     unique_digits++;
  43.                     break;
  44.                 }
  45.             }
  46.         }
  47.         multiplication = 1;
  48.     }
  49.  
  50.     printf("unique digits: %d\n",unique_digits);
  51.     printf("uniqe digits are: ");
  52.     for(i=0; i < unique_digits; i++)
  53.     {
  54.         printf("%d ",b[i]);
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement