fork download
  1. #include <stdio.h>
  2.  
  3. int intDiv(int num, int den) {
  4. int count = 0;
  5. if (den <= 0)
  6. return -1; /* Div not possible */
  7. while (num >= den) {
  8. num -= den;
  9. count++;
  10. }
  11. return count;
  12. }
  13.  
  14. int divide(int n, int d) {
  15. if(d <= 0)
  16. return -1;
  17. int k = d;
  18. int i, c, index=1;
  19. c = 0;
  20. while(n > d){
  21. d <<= 1;
  22. index <<= 1;
  23. }
  24. while(1){
  25. if(k > n)
  26. return c;
  27. if(n >= d){
  28. c |= index;
  29. n -= d;
  30. }
  31. index >>= 1;
  32. d >>= 1;
  33. }
  34. return c;
  35. }
  36.  
  37. int main()
  38. {
  39. printf("%d %d %d %d\n", intDiv(4,5), intDiv(13,5), intDiv(20,4), intDiv(3,0));
  40. printf("%d %d %d %d\n", divide(4,5), divide(13,5), divide(20,4), divide(3,0));
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
0 2 5 -1
0 2 5 -1