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

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    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. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    while (n) { sum += n%10; n /= 10; }

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Thanks! Cboard is a great place to get homework done.

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Duh!
    That was your home work?

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

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by manav View Post
    Duh!
    That was your home work?

    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. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I suggest you read the rules before doing that again, http://cboard.cprogramming.com/annou...t.php?f=4&a=39

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    yeah i know, project euler questions are very booooooooooooooooring!

  8. #8
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by zacs7 View Post
    I suggest you read the rules before doing that again, http://cboard.cprogramming.com/annou...t.php?f=4&a=39
    please do not ask people to do your entire homework for you
    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. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, but that's what you were hoping for "I always get my boring homework done by others"

  10. #10
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Sorry

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    you are welcome abk. next time, just PM me. i will do your home work. $0.5 per line of code.

  12. #12
    coder
    Join Date
    Feb 2008
    Posts
    127
    manav, you should write your code so then
    Code:
    while (n)
    {
       sum += n%10;
       n /= 10;
    }

  13. #13
    Banned
    Join Date
    Nov 2007
    Posts
    678
    no, like this:
    Code:
    while (n)
    {
      d = n%10;
      sum += d;
      n /= 10; 
    }
    $3.0 please!

  14. #14
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    Code:
    while (n)
    {
      d = n%10;
      sum += d;
      n /= 10; 
    }


    dz wOrkz...!!

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    ajayd, u want complete code? PM me! $0.5 per line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. To get the sum of digits of a five digit number
    By hum_tum2005007 in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 04:39 PM
  3. how to add 2 digits?
    By nefsan in forum C Programming
    Replies: 16
    Last Post: 03-28-2008, 02:41 PM
  4. Replies: 7
    Last Post: 05-26-2003, 05:44 PM
  5. Finding digits in a number
    By MadStrum! in forum C Programming
    Replies: 5
    Last Post: 09-05-2002, 03:05 AM