How to sum the digits of an integer

I wrote a function, inspired by code I found in a C++ tutorial, to sum the digits of an integer:

int checksum(int num) {
  int sum = 0;
  while (num > 0) {
    sum = sum + num % 10;
    num = num / 10;
  }
  return sum;
}

For some reason this function always returns 0.

What am I doing wrong?

What am I doing wrong?

Always passing it zero?

Use some Serial prints to track the values of the variables while the function runs. You may be able to see what is happening.

Works fine for me.

DKWatson:
Works fine for me.

Same here. I thought I was doing something wrong.

Must be a problem in the calling instruction that we can't see.

Let me guess - you're ignoring/letting the local variable that receives the return value go out of scope, and printing the value of the global with the same name.

AWOL:
Let me guess - you're ignoring/letting the local variable that receives the return value go out of scope, and printing the value of the global with the same name.

?
I did this,

int checksum(int num) 
{
    int sum = 0;
    while (num > 0) 
    {
        sum += num % 10;
        num /= 10;
    }
    return sum;
}

int main()
{
    prtiln(checksum(123123));
}

and got 12.

@DKWatson, my comment was aimed at the snippet-poster, not you.

Ah so. Me understand'em now.

groundFungus:
Use some Serial prints to track the values of the variables while the function runs. You may be able to see what is happening.

Thank you.

I printed the "num" variable and discovered it is NOT the integer I fed it with! It is some negative integer.

Must be some problem with my typecasting. I feed the function with a 60-digit string which I typecast to integer like this:

checksum(mystring.toInt());

mystring is: 153934992015393499801539350040153934788014420636401539350040

That behavior is very strange, toInt() had never failed me before.

Any ideas how to overcome this issue?

153934992015393499801539350040153934788014420636401539350040 ?

Are you for real?

That's a 197 bit number.

DKWatson:
and got 12.

Your program does not get even compiled -- how have you got 12?

AWOL:
153934992015393499801539350040153934788014420636401539350040 ?

Are you for real?

That’s a 197 bit number.

Can you tell it’s been 10 years since I did any serious programming? :smiley:

What data type should I use? Or should I first break up that long string into an int array of single digits, and then sum the members of the array?

AWOL:
@DKWatson, my comment was aimed at the snippet-poster, not you.

Correct. The OP has not posted his full codes. Now, I am filling up the gap:

void setup()
{
  Serial.begin(9600);
  int x = checksum(0x7FFF);
  Serial.print(x, DEC); //32767 ---> 3+2+7+6+7 = 25
}

void loop()
{
  
}

int checksum(int num) 
{
  int sum = 0;
  while (num > 0) 
  {
    sum = sum + num % 10;
    num = num / 10;
  }
  return sum;
}

Leave it as a String (ugh, can't believe I just wrote that) and sum the digits.

Just AND the ASCII codes with 0x0F and sum the result.

AWOL:
Leave it as a String (ugh, can't believe I just wrote that) and sum the digits.

How?

Consider using the Big Number library.

Or, as a character array:

char num_string[]="123456";

void setup() {
Serial.begin(9600);
long sum=0;
for(int i=0; i<strlen(num_string); i++) {
   sum += num_string[i]-'0';
   }
Serial.println(sum);
}

void loop() {}

jremington:
Or, as a character array:

Your program (a corollary of Post#14 @AWOL) works very well for the OP’s string – “153934992015393499801539350040153934788014420636401539350040”.

When all the digits of the above string are added manually, it is: 234; your program also returns 234.

jremington:
Consider using the Big Number library.

Or, as a character array:

char num_string[]="123456";

void setup() {
Serial.begin(9600);
long sum=0;
for(int i=0; i<strlen(num_string); i++) {
  sum += num_string[i]-‘0’;
  }
Serial.println(sum);
}

void loop() {}

Great, thanks! May I ask what’s the purpose of the “-‘0’” inside the loop?

The ascii value of the character '0' is not 0. This converts the ascii '0' to decimal number value 0.