Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //*
- // Version itérative
- int digitsSum(int nb)
- {
- int sum=0;
- while(nb>0)
- {
- sum += nb%10;
- nb /= 10;
- }
- return sum;
- }
- // */
- //*
- // Version récursive
- int digitsSum(int nb)
- {
- if(nb==0)
- return 0;
- return (nb%10) + digitsSum(nb/10);
- }
- // */
- int main(void)
- {
- int input;
- scanf("%d", &input);
- printf("%d\n", digitsSum(input));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement