# Thread: Sum of digits of an integer: Odd or Even?

1. ## Sum of digits of an integer: Odd or Even?

Hey, I've been trying to work out a function for this for a good while, I'm brand new to loops so I'm not sure which would be the best to use to separate the digits out to add. I do know from some searching around that I need to do:

number % 10 to get the digit alone

number / 10 to strip away the right-most number, I'm just having troubles with looping it.

2. I'm sure any type of loop will do, it doesn't matter.

>> number / 10 to strip away the right-most number, I'm just having troubles with looping it.
Think about what that means, though. All positive numbers have a lower bound of zero, so when are you done?

3. Would you be done when number < 1?

4. If number is always positive, yes.

5. Could I do something like:
Code:
```{
int number = 0, digit = 0;

for(number != 0)
{
digit = number % 10;

number = number / 10;
}

}```

6. Well, I got the loop figured out, but now I'm having trouble adding the digits of the integer together (eg. 1987; 1+9+8+7). I don't really know how to store or save the digits after they have been singled out. Any help is much appreciated!

7. When you get the first digit, you would add it to the sum of zero. And then start adding more digits until you run out.

sum = sum + digit

8. Ok, thanks for the help! Would this function do the trick?

Code:
```int isOddEven(int intNumber)
{
int number = 0, digit = 0, sum = 0;

while(number != 0)
{
digit = number % 10;

sum = sum + digit

number = number / 10;
}

return sum;

}```

9. Originally Posted by Devolution
Ok, thanks for the help! Would this function do the trick?

Code:
```int isOddEven(int intNumber)
{
int number = 0, digit = 0, sum = 0;

while(number != 0)
{
digit = number % 10;

sum = sum + digit

number = number / 10;
}

return sum;

}```
Apart from a missing ";" and the fact that the name of the function is "isOddEven" but returns the sum of the digits, and the fact that you will always return 0 since you're using the local variable number instead of the parameter passed into the function.

10. Oops, yea I got the function name switched up with another one.

How do I return the parameter passed into the function? I thought returning sum would do that, but I'm pretty new to C, and things haven't "clicked" for me yet I'm trying my best to understand so I don't fall behind.

11. Originally Posted by Devolution
Oops, yea I got the function name switched up with another one.

How do I return the parameter passed into the function? I thought returning sum would do that, but I'm pretty new to C, and things haven't "clicked" for me yet I'm trying my best to understand so I don't fall behind.
You don't want to return the parameter passed into the function, and I didn't suggest you do so. I suggest you use the parameter passed into the function, instead of completely ignoring it in favor of calculating the sum of the digits of 0 (a/k/a number) instead.

12. Ok I believe I understand what you're saying now, correct me if I'm wrong though. The "number" variable in there too was another mix-up that I didn't mean to have in the function, but thanks for pointing it out. Fixed up, it looks like this:

Code:
```int sumDigits(int intNumber)
{
int digit = 0, sum = 0;

while(intNumber != 0)
{
digit = intNumber % 10;

sum = sum + digit;

intNumber = intNumber / 10;
}
return sum;
}```

13. I don't really see the point of your function.
To check if a number is even or odd, you just use the % operator and specify a certain number afterwards, and that number isn't 10.

14. We're checking to see if the sum of the digits of an integer are even or odd.

15. Originally Posted by Devolution
Ok I believe I understand what you're saying now, correct me if I'm wrong though. The "number" variable in there too was another mix-up that I didn't mean to have in the function, but thanks for pointing it out. Fixed up, it looks like this:

Code:
```int sumDigits(int intNumber)
{
int digit = 0, sum = 0;

while(intNumber != 0)
{
digit = intNumber % 10;

sum = sum + digit;

intNumber = intNumber / 10;
}
return sum;
}```
instead of "sum = sum + digit;" you can just write "sum += digit;"
same thing for intNumber "intNumber /= 10;"

saves a little bit of typing