-7

For instance , the number is 123, the answer is 6. Only recursion!

djot
  • 2,952
  • 4
  • 19
  • 28
  • 3
    Welcome to Stack Overflow! We like to help, but we want to see you make an attempt first. What have you tried, and where are you stuck? – Michael Petrotta Aug 31 '12 at 01:09

3 Answers3

4

While @SamGrondahl is on the right track, unfortunately, if you enter a negative number it gives unexpected results. This should work properly with negative numbers:

int sumdigits(int number) {
    int sign = number < 0 ? -1 : 1;
    number = sign * number; // abs the number

    if (number < 10) 
        return sign * number;
    else 
        return sign * (number % 10 + sumdigits(number / 10));
}

This will return the sum of the digits, and if the number is negative, negate that.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
1

A correct answer is trickier to achieve than at first glance, since the negation of INT_MIN may not fit in an int. The posted solutions would work for the most part by changing their implementation to use unsigned int during recursion. Below is an alternative to that.

int sum_digits_recursively (char digits[], int i) {
    if (i == 0 && digits[i] == '-') return -sum_digits_recursively(digits, 1);
    if (digits[i] == '\0') return 0;
    return (digits[i]-'0') + sum_digits_recursively(digits, i+1);
}

int sum_digits (int x) {
    char digits[sizeof(x) * CHAR_BIT];
    snprintf(digits, sizeof(digits), "%d", x);
    return sum_digits_recursively(digits, 0);
}
jxh
  • 69,070
  • 8
  • 110
  • 193
-1

This should do what you want:

int recurse( int number ) {
    if ( abs(number) < 10 ) return number;
    return number % 10 + recurse ( number / 10 );
}
Sam Grondahl
  • 2,397
  • 2
  • 20
  • 26
  • What if the number is negative? Gives unexpected results then. – Richard J. Ross III Aug 31 '12 at 01:10
  • Why would you sum the digits of a negative number? – Sam Grondahl Aug 31 '12 at 01:11
  • Taking it to an absolute value doesn't change anything. If the number was -213, I would expect 2, not 6. – Makoto Aug 31 '12 at 01:12
  • 1
    No, you would expect -6. The negative operator applies to all trailing digits. Otherwise -213 would mean -200 + 10 + 3. – Sam Grondahl Aug 31 '12 at 01:15
  • @MichaelBurr: I wouldn't expect -6 from -213. I wish to transform -213 into -2+1+3 instead. It just means that the highest order value could detract from your overall sum. There is a way to do that. – Makoto Aug 31 '12 at 01:18
  • humm, whats the meaning of abs? also i only need it for positive numbers so Sam's answer is useful! – Gonzalo Antonicelli Aug 31 '12 at 01:18
  • int abs ( int number ) { int result = number < 0 ? -1 * number : number; return result } – Sam Grondahl Aug 31 '12 at 01:19
  • @GonzaloAntonicelli abs means `absolute value`. His answer is still incorrect for negative numbers, though. – Richard J. Ross III Aug 31 '12 at 01:20
  • Exactly: "@MichaelBurr: I wouldn't expect -6 from -213. I wish to transform -213 into -2+1+3 instead. It just means that the highest order value could detract from your overall sum. There is a way to do that." Doesn't make sense to me, but perhaps the right way in any case. – Sam Grondahl Aug 31 '12 at 04:26