Thread: how to sum the digits of a number?

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Going fine so far. Now you need to make use of recursion passing sumb.

    Of course usually everything adds up to 23 and you start losing your mind...

    (for those that don't get it, it was a reference to a certain Jim Carey movie)
    Tis true though - Both my and my partners date of birth add up to 23
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use recursion only loop
    what to do?

  3. #18
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is no sense doing digit = something and digits = something else. You always get something else. Delete the red line.
    Code:
    printf("enter digit");
    scanf("%d",&digit);
    do
    {
    sumb += digit % 10;
    digit=digit-digit%10;
    digit = digit / 10;
    }while(digit / 10 >1);
    printf("%d",sumb);
    So you get the first part. The sum of all digits. Now you need to do the same method with sumb. So, make a function with your algorithm in which you will pass digits. Then use a loop with the function.
    Code:
    int sumDigits(int digit) {... return sumb}
    int main()
    {
       int result = 0;
       ...
        for (...) {
             ...
             result = sumDigits(...)
         }
         printf("Sum =%d", 23);
    }

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I cant use recursion or function call.

    i tried to built this one
    but for 679 it gives me 7
    i dont know why?
    Code:
     printf(" enter number  ");
    
    scanf("%d",&digit);
    copy = digit;
    
    
    do
    {
    sumb=sumb+copy%10;
    
    copy=copy/10;
    
    if (sumb<10){
        flag=1;
    }
    
    else
    {
    copy=sumb;
    sumb=0;
    }
    
    }while((copy!=0)&&(flag=1));

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You set flag=1 the first time through, and it never gets cleared when the sum goes over 10.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no
    i declared
    int flag=0;
    and i put flag=1 only if the sum under 10

    where is the mistake

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    I cant use recursion or function call.
    My hint would be to use two variables and two nested (while) loops. One variable would be used to store the original input while the other would be a temporary to store the current sum. The outer loop would be used to keep looping until the sum is less than 10. The inner loop would be used to compute the current sum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what about my code
    how to fix it
    ?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please indent your code. You should also post the smallest and simplest program that demonstrates the error.

    One possible problem is that you write (flag=1) when you probably mean (flag == 1). However, it would be good if you stated what is the algorithm that you are trying to implement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is it intended now?
    i fixed the "==" thing
    its still doesnt work
    ??

    Code:
     printf(" enter number  ");
    
    scanf("%d",&digit);
    copy = digit;
    
    
    do
    {
    sumb=sumb+copy%10;
    copy=copy/10;
    if (sumb<10){
        flag=1;
    }
    
    else
    {
        copy=sumb;
         sumb=0;
    }
    }while((copy!=0)&&(flag==1));

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is it so hard to indent the code?
    Code:
    printf(" enter number  ");
    
    scanf("%d", &digit);
    copy = digit;
    
    do
    {
        sumb = sumb + copy % 10;
    
        copy = copy / 10;
    
        if (sumb < 10)
        {
            flag = 1;
        }
        else
        {
            copy = sumb;
            sumb = 0;
        }
    
    } while ((copy != 0) && (flag == 1));
    Considering that you have printed the wrong result before, I strongly suggest that you post the smallest and simplest compilable program that demonstrates the error (and state what exactly is the error).

    As for the fix: I think you actually should write flag != 1 instead, but that's the problem when you use such a generic name as "flag". We do not know what exactly is the intended meaning of the flag being set.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So trace through the !@#$^@#$% thing already. Suppose copy = 679. I assume you managed to set sumb = 0 before we start.
    First time through: sumb = 9; copy is 67; flag is set to 1 since sumb is less than 10.
    Second time through: sumb = 16; copy is 6; copy is set to 16 and sumb is set back to 0 -- even through we're not done with the number yet! -- and flag is still 1!
    Third time through: sumb = 6; copy is 1; flag is set to 1 since sumb is less than 10.
    Fourth time through: sumb = 7; copy is 0; flag is set to 1 once again, and now the while loop stops.

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need it to be 679-> 2
    i cant see where the mistake in this algorithm
    ??

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i need it to be 679-> 2
    How do you get that? The recursive sum of the digits of 679 is 4.

    Quote Originally Posted by transgalactic2
    i cant see where the mistake in this code
    As tabstop has pointed out, trace your code. Personally, I feel that you should go with what I suggested. In my opinion, it is easier to implement it using nested loops than trying to figure out the exact conditions for a single loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Dude! You have this code:
    Code:
    do
    {
        sumb += digit % 10;
        digit = digit / 10;
    }while(digit / 10 >1);
    And you get the sum of all digits.
    You want to do this again for the new sumb. So you want to do digit = sumb and do this again.
    Like:
    digit = 543
    sumb = 543 = 5 + 4 + 3 = 12

    digit = sumb = 12
    sumb = 1 + 2 = 3

    Try that in a double while loop. In the end print sumb

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, 10:13 AM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Help on Sum of Digits Entered
    By SkinnyK in forum C Programming
    Replies: 5
    Last Post: 05-14-2008, 05:16 AM