Advertisement
Anthei

Playing with Requirements

May 22nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int checkDupe(int);
  6. int checkSquare(int);
  7. int checkZeros(int);
  8.  
  9. int main(void)
  10. {
  11.     int index;
  12.     int counter = 0;
  13.  
  14.     for (index = 111111111; index < 1000000000; index++)
  15.     {
  16.  
  17.         if ((checkZeros(index) == 1) && (checkSquare(index) == 1)
  18.             && (checkDupe(index) == 1))
  19.         {
  20.             counter++;
  21.             printf("%d. %d\n", counter, index);
  22.         }
  23.  
  24.     }
  25. }
  26.  
  27. int checkDupe(int number)
  28. {
  29.     char strNum[10];
  30.     char charNum;
  31.     int dupeArr[10];
  32.     int numsPlace[10];
  33.     int index, dupes = 0;
  34.     int isValid = 1;
  35.  
  36.     //convert to a string
  37.     for(index = 9; index >= 0; index--)
  38.     {
  39.         numsPlace[index] = number % 10;
  40.         number = number / 10;
  41.         strNum[index] = numsPlace[index] + 48;
  42.     }
  43.  
  44.     //initialize the array. 10 = intArray.length
  45.     for (index = 0; index < 10; index++)
  46.     {
  47.         dupeArr[index] = 0;
  48.     }
  49.  
  50.     //count how many of each number are in the string
  51.     for (index = 0; index < strlen(strNum); index++)
  52.     {
  53.         charNum = strNum[index];
  54.  
  55.         switch (charNum)
  56.         {
  57.             case '0':
  58.                 dupeArr[0]++;
  59.                 break;
  60.             case '1':
  61.                 dupeArr[1]++;
  62.                 break;
  63.             case '2':
  64.                 dupeArr[2]++;
  65.                 break;
  66.             case '3':
  67.                 dupeArr[3]++;
  68.                 break;
  69.             case '4':
  70.                 dupeArr[4]++;
  71.                 break;
  72.             case '5':
  73.                 dupeArr[5]++;
  74.                 break;
  75.             case '6':
  76.                 dupeArr[6]++;
  77.                 break;
  78.             case '7':
  79.                 dupeArr[7]++;
  80.                 break;
  81.             case '8':
  82.                 dupeArr[8]++;
  83.                 break;
  84.             case '9':
  85.                 dupeArr[9]++;
  86.                 break;
  87.             default:
  88.                 break;
  89.         }
  90.     }
  91.  
  92.     //If there is more than 1 in any place, it's a duplicate
  93.     for (index = 0; index < 10; index++)
  94.     {
  95.         if (dupeArr[index] >= 2)
  96.         {
  97.             isValid = 0;
  98.         }
  99.     }
  100.  
  101.     return isValid;
  102. }
  103.  
  104. int checkZeros(int number)
  105. {
  106.     char strNum[10];
  107.     char charNum;
  108.     int index;
  109.     int numsPlace[9];
  110.     int isValid = 1;
  111.  
  112.     //convert to a string
  113.     for(index = 8; index >= 0; index--)
  114.     {
  115.         numsPlace[index] = number % 10;
  116.         number = number / 10;
  117.         strNum[index] = numsPlace[index] + 48;
  118.     }
  119.  
  120.     //check if there are any zeros
  121.     for (index = 0; index < 10; index++)
  122.     {
  123.         charNum = strNum[index];
  124.         if (charNum == '0')
  125.             isValid = 0;
  126.     }
  127.  
  128.     return isValid;
  129. }
  130.  
  131. int checkSquare(int number)
  132. {
  133.     int sqRoot = (int)sqrt(number);
  134.     int isPerfect = 0;
  135.  
  136.     if ((sqRoot * sqRoot) == number)
  137.         isPerfect = 1;
  138.     else
  139.         isPerfect = 0;
  140.  
  141.     return isPerfect;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement