how to sum the digits of a number?

This is a discussion on how to sum the digits of a number? within the C Programming forums, part of the General Programming Boards category; if the input is 695 6+9+5 = 20 2 + 0 = 2. i need to get the output 2 ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to sum the digits of a number?

    if the input is 695
    6+9+5 = 20
    2 + 0 = 2.

    i need to get the output 2

    ??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What have you tried so far?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Well... if the input is always within the range of say, an unsigned int, then you just store it as an unsigned int and compute the remainder when the number is divided by 9. If the input is not zero yet the remainder is 0, then the recursive sum of digits is 9, otherwise it is the remainder.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    cccccc

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried this code

    but after i enter the input number
    its just freezes

    ??

    Code:
     	printf("enter digit");
    scanf("%d",&digit);
    while((digit/10)>1){
    sumb=sumb+digit%10;
    }
    
    printf("%d",digit);

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Since digit is never actually changed, the loop is an infinite loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it tried this code
    but it gives me 6
    on 695 input

    i should give me 2
    ??
    Code:
    printf("enter digit");
    scanf("%d",&digit);
    while((digit/10)>1){
    sumb=sumb+digit%10;
    
    digit=digit-digit%10;
    digit=digit/10;
    }
    
    printf("%d",digit);

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    If you're trying to find out the sum, why are you printing out the digits?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Well, your algorithm is actually a little wrong. Okay, instead of trying to compute the recursive sum of digits, first try computing the sum of digits. Show a more complete program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    c
    ccc

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont know what the algorithm for doing this

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    As I said, first implement an algorithm that sums the digits of the number. Get this right, otherwise you would not be able to implement an algorithm that recursively sums the digits of the number (unless you use the "cheating" method I suggested).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok
    this this the code i came up with
    each time i add the remainder
    i subtract the remainder from the number
    divide it by 10
    and get the next remainder

    it very logical to me

    where is the mistake?
    Code:
    printf("enter digit");
    scanf("%d",&digit);
    while((digit/10)>1){
    sumb=sumb+digit%10;
    
    digit=digit-digit%10;
    digit=digit/10;
    }
    
    printf("%d",digit);

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And where are you going to add the leading digit into your sum?

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    like this?
    Code:
    printf("enter digit");
    scanf("%d",&digit);
    do
    {
    sumb=sumb+digit%10;
    
    digit=digit-digit%10;
    digit=digit/10;
    }while((digit/10)>1);
    
    printf("%d",sumb);

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of digits of an integer: Odd or Even?
    By Devolution in forum C Programming
    Replies: 14
    Last Post: 03-06-2009, 06:24 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  5. Help on Sum of Digits Entered
    By SkinnyK in forum C Programming
    Replies: 5
    Last Post: 05-14-2008, 06:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21