# Thread: How do I add the digits of a number ?

1. ## How do I add the digits of a number ?

Code:
`int num =5648;`

How do i get 5+6+4+8 easily ?

How to get all the digits separately in different variables and then add them ?

By "easily" I mean without using special functions.

2. Code:
`while (n) { sum += n&#37;10; n /= 10; }`

3. Thanks! Cboard is a great place to get homework done.

4. Duh!

If you wanted to reverse a positive number? You are welcome!

5. Originally Posted by manav
Duh!

If you wanted to reverse a positive number? You are welcome!
yes that was my homework. I always get my boring homework done by others and solve only project euler questions.

6. I suggest you read the rules before doing that again, http://cboard.cprogramming.com/annou...t.php?f=4&a=39

7. yeah i know, project euler questions are very booooooooooooooooring!

8. Originally Posted by zacs7
I suggest you read the rules before doing that again, http://cboard.cprogramming.com/annou...t.php?f=4&a=39
I never asked anyone to do my complete homework.

How to get all the digits separately in different variables and then add them ?
I just asked for a hint. What can I do if someone comes and writes the piece of code ?

Anyway, sorry. (I don't want to argue any more)

9. No, but that's what you were hoping for "I always get my boring homework done by others"

10. Sorry

11. you are welcome abk. next time, just PM me. i will do your home work. \$0.5 per line of code.

12. manav, you should write your code so then
Code:
```while (n)
{
sum += n&#37;10;
n /= 10;
}```

13. no, like this:
Code:
```while (n)
{
d = n&#37;10;
sum += d;
n /= 10;
}```

14. Code:
```while (n)
{
d = n&#37;10;
sum += d;
n /= 10;
}```

dz wOrkz...!!

15. ajayd, u want complete code? PM me! \$0.5 per line