-3

here's my task: I want to sum the digits of an int, problem is I don't know how many digits there are (and I can't ask the user). Is there anything like a getchar for int? I've also tried int c = getchar(), but it isn't working. Thanks:)

I wanted it to be a recursive function, but I guess it is just wrong.

I THINK I GOT IT, THANKS EVERYONE!

int sum_digits (int n) {
int answer = 0;
if (getchar() == 0) {
    return answer;
}
else {
    int c = getchar();
    return answer = c + sum_digits(n);
}

}

bananabread76
  • 37
  • 1
  • 1
  • 9
  • 1
    Show what you have tried – Gopi Feb 26 '15 at 10:49
  • There was a question that was similar to the past. – BLUEPIXY Feb 26 '15 at 10:51
  • `0b 11111111 11111111 11111111 11111111` or `0xFFFF` or `65535`? Notice all the **representations** are for the same value. – pmg Feb 26 '15 at 10:52
  • 1
    you can solve this by using the modulo operator in combination with division. (i.e. calculate modulo 10 to get the value of the last digit, then devide the number by 10, then again modulo 10, etc.) – ashiaka Feb 26 '15 at 10:52
  • What @ashiaka says. Time for some recursion - keeps the prof happy. – Martin James Feb 26 '15 at 10:52
  • I can personally testify that "int c = getchar()" works the way it should. You are using it for another (wrong) purpose, which is generally not recommended. – Jongware Feb 26 '15 at 10:53
  • @ashiaka I have read that answer before, but I just don't see how that can be recursive. Like how do I know when to stop diving If I don't know how many digits there are and if I can't count digits separately and give that value to my function as a parameter? – bananabread76 Feb 26 '15 at 10:57
  • divide by ten, then by ten, etc. until 0, you will be able to compute the number of digits! Ex: 1234/10 = 123, 123/10=12, 12/10=1, 1/10=0, fours divisions by ten, then four digits. – Jean-Baptiste Yunès Feb 26 '15 at 11:01
  • @SCoder How many digits are there in the number 0? – unwind Feb 26 '15 at 11:01
  • Hey, this is not the sum of the digits of an int! This is the sum of the digits of the char representation of an int... And your answer is wrong! – Jean-Baptiste Yunès Feb 26 '15 at 11:03
  • 1, I understand my mistake. – bananabread76 Feb 26 '15 at 11:03
  • @pmg `0b 11111111 11111111 11111111 11111111` --> `0b 11111111 11111111` – BLUEPIXY Feb 26 '15 at 12:46

3 Answers3

2

Take any int, say n, and compute n%10 (the remainder of the division by ten) and n/10 (the quotient of the division by ten). The remainder is exactly the last digit, and the quotient the number without its last digit... Loop, until the quotient is zero and make a sum at each step.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
2

Try the below program.

int sum_digits (unsigned int n)
{
    int sum = 0;
    while (n > 0) {
        sum += (n % 10);
        n /= 10;
    }
    return sum;
}

EDIT: Recursive function:

int sum_digits (unsigned int n)
{
    if (n < 10)
        return n % 10;
    return (n % 10) + sum_digits(n / 10);
}
Santosh A
  • 5,173
  • 27
  • 37
1

For the golf, here's a recursive approach:

unsigned int countDigits(unsigned int x)
{
  if (x < 10)
    return 1;
  return 1 + countDigits(x / 10);
}
unwind
  • 391,730
  • 64
  • 469
  • 606