Thread: Sum of digits in C

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    19

    Sum of digits in C

    Hello,

    I have written in C a program that calculate the sum of digits (for a number).

    For Example for the number 199 the checksum is 19.

    But now i want that my program also calculate the checksum of 19.

    How can i do that in C? Can i make a nested function in C?

    Here is my code

    Code:
    int dsum(int z)
    {
        int q=0;
        while(z>0)
        {
            q+= z%10;
            z /=10;
        }
        return q;
    }
    
    
    
    int main(){
        int z =199;
        int i;
        int qs= dsum(z);
        printf("%d", qs);
    
    return 0;}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,416
    What's your question?
    Your code gives the right answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    19
    For my example the checksum is 19. But i want now that my function dsum calculate the checksum again, until the sum of the digits contains only one number

    For example:
    199 => 19
    19 => 10
    10 => 1

    In that case my solution is = 1

    For me is not very clear how i can extend my function to do that.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,416
    Simples!
    Code:
    qs = z;
    do {
      qs = dsum(qs);
    } while ( qs >= 10 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,160
    In this particular case there happens to be a mathematical shortcut:
    Code:
    int dsum(int z)
    {
        if (z == 0)
        {
            return 0;
        }
        int q = z % 9;
        return (q == 0) ? 9 : q;
    }
    (though you need to decide what should happen if the input is a negative number)
    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

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,042
    Quote Originally Posted by laserlight View Post
    In this particular case there happens to be a mathematical shortcut
    Witchcraft!
    How does that work?
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,160
    Informally, consider that (10^i)d = d (mod 9) for a non-negative integer i and a decimal digit d (but of course 9 = 0 (mod 9)), where ^ denotes exponentiation and = denotes congruence.

    Now, suppose we're trying to (non-recursively) sum the digits of an N digit non-negative integer x = (10^0)d_0 + (10^1)d_1 + ... + (10^i)d_i + ... + (10^(N-1))d_(N-1). This means we want to compute (d_0 + d_1 + ... + d_(N-1)). Since (10^i)d = d (mod 9), it follows that (10^0)d_0 + (10^1)d_1 + ... + (10^i)d_i + ... + (10^(N-1))d = (d_0 + d_1 + ... + d_i + ... + d_(N-1)) (mod 9).

    But (d_0 + d_1 + ... + d_i + ... + d_(N-1)) is itself an integer y = (10^0)d_0 + (10^1)d_1 + ... + (10^i)d_i + ... + (10^(N-1))d_(N-1) for another value of N and another sequence of digits d_i. So it follows that y = (d_0 + d_1 + ... + d_i + ... + d_(N-1)) (mod 9) for this new sequence of digits, so on and so forth. Hence computing the remainder of x divided by 9 gives us the recursive sum of digits of x, with the caveat that when x is not 0, a remainder of 0 means that the recursive sum of digits is 9, as per the earlier observation that 9 = 0 (mod 9).
    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. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,042
    That's very interesting. After reading it through carefully I guess I understand it, but it still seems like magic.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  9. #9
    Registered User
    Join Date
    Nov 2020
    Posts
    19
    Quote Originally Posted by Salem View Post
    Simples!
    Code:
    qs = z;
    do {
      qs = dsum(qs);
    } while ( qs >= 10 );

    Thank you. It is clear now for me. To do while loop is very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on sum digits in C
    By KevinH123 in forum C Programming
    Replies: 7
    Last Post: 12-07-2010, 01:36 AM
  2. Help with digits
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-03-2007, 06:04 PM
  3. get all the right digits
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 11-27-2005, 06:16 AM
  4. Two digits
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2004, 01:53 PM
  5. Getting digits from int.
    By aker_y3k in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2003, 12:45 PM

Tags for this Thread