Advertisement
tobast

Untitled

Oct 14th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //*
  5. // Version itérative
  6. int digitsSum(int nb)
  7. {
  8.     int sum=0;
  9.     while(nb>0)
  10.     {
  11.         sum += nb%10;
  12.         nb /= 10;
  13.     }
  14.     return sum;
  15. }
  16. // */
  17.  
  18. //*
  19. // Version récursive
  20. int digitsSum(int nb)
  21. {
  22.     if(nb==0)
  23.         return 0;
  24.     return (nb%10) + digitsSum(nb/10);
  25. }
  26. // */
  27.  
  28. int main(void)
  29. {
  30.     int input;
  31.     scanf("%d", &input);
  32.     printf("%d\n", digitsSum(input));
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement