Advertisement
ben1939

תרגיל 4 2

Nov 26th, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. int isBaseFour(int num)
  6. {
  7. int a;
  8.   a = num % 10;
  9.  
  10.    if (num == 0)
  11.    return 1;
  12.  
  13.     if (a>3 || a<0)
  14.    return 0;
  15.  
  16.   return isBaseFour(num/10);
  17. }
  18.  
  19.  
  20. int toDecimalIterate(int num)
  21. {
  22. int sum = 0;
  23.    if (isBaseFour(num) == 0)            
  24.    return -1;
  25.  
  26.     for (int i=0; num > 0; i++) {
  27.      sum = sum + (num % 10) * pow(4.0, i);
  28.      num = num/10 ;     }
  29.        
  30.      return sum;
  31.   }
  32.  
  33. int four (int num , int i=0)
  34. {
  35. int a ;  
  36.     if (num == 0)
  37.     return 0;
  38.  
  39.   a = (num % 10) * pow(4.0, i);
  40.      
  41.  return a + four(num/10, i++);
  42.  }
  43.  
  44.  
  45. int toDecimal(int num)
  46. {
  47.      if (isBaseFour(num) == 0)
  48.      return -1;
  49.  
  50.  return four(num);
  51.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement