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

• 03-06-2009
Devolution
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.

• 03-06-2009
whiteflags
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?
• 03-06-2009
Devolution
Would you be done when number < 1?
• 03-06-2009
whiteflags
If number is always positive, yes.
• 03-06-2009
Devolution
Could I do something like:
Code:

```{         int number = 0, digit = 0;         for(number != 0)         {                 digit = number % 10;                 number = number / 10;         } }```
• 03-06-2009
Devolution
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!
• 03-06-2009
whiteflags
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
• 03-06-2009
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; }```
• 03-06-2009
tabstop
Quote:

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.
• 03-06-2009
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.
• 03-06-2009
tabstop
Quote:

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.
• 03-06-2009
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; }```
• 03-06-2009
cpjust
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.
• 03-06-2009
Devolution
We're checking to see if the sum of the digits of an integer are even or odd.
• 03-06-2009
ಠ_ಠ
Quote:

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