fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int teste1 = carries(9, 1);
  5. int teste2 = carries(99, 1);
  6. int teste3 = carries(999, 1);
  7. int teste4 = carries(999, 10);
  8.  
  9. printf("9+1: %i carries\n", teste1);
  10. printf("99+1: %i carries\n", teste2);
  11. printf("999+1: %i carries\n", teste3);
  12. printf("999+10: %i carries\n", teste4);
  13.  
  14. return 0;
  15. }
  16.  
  17. int carries(int a, int b) {
  18. return (sumDigits(a) + sumDigits(b) - sumDigits(a+b)) / 9;
  19. }
  20.  
  21. int sumDigits(int num) {
  22. int sum = 0;
  23. while (num != 0) {
  24. sum += num % 10;
  25. num /= 10;
  26. }
  27. return sum;
  28. }
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
9+1: 1 carries
99+1: 2 carries
999+1: 3 carries
999+10: 2 carries