Need help for repetition method!

This is a discussion on Need help for repetition method! within the C Programming forums, part of the General Programming Boards category; Hi guys, I have a question to ask: Write a program to read in a positive integer and add up ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Need help for repetition method!

    Hi guys,

    I have a question to ask:

    Write a program to read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits (1 + 2 + 3 + 4 + 5) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6.

    The tricky part for me is the part where the user can type in any digits he wishes.

    From the angle i see it, I attempted to do smth along this line:

    while x > 1 // x is the number the user entered
    x = x/10
    count++

    using the count i know exactly how many digits are there, from then i proceed to identify each digit,

    x % pow(10,n) * 10 to isolate each digit due to the correlation

    my question is how to i assign the values i looped out to another variable? like digit1 = 2, digit 2 =2 in this looping function?

    thanks for ur help!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You could use some logic like this:
    Code:
    read the number from the user
    while the number is > 9:
        sum up the digits of the number, and store the result back into the same variable
    where the "sum up the digits of the number" step consists of the while loop you just described. Basically if you use a while loop inside a while loop you should be able to solve this problem without too much difficulty.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    the most efficient way is this..

    Code:
    if (number > 9)
        number = number % 9;
    
    return number;
    here the number is what the use inputs... for eg. if number = 12345, then 12345%9 = 6

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    xabhi: that is also incorrect
    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

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    xabhi: that is also incorrect
    no it's not...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by xabhi
    no it's not...
    You added a check for integers greater than 9, but missed out an important check for multiples of nine. For example, try 123453.
    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
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    seriously.. what is wrong with me.. i'm not that stupid you know...

    if (number < 9)
    return number;

    number = number % 9;
    if (number == 0)
    return 9;

    return number;

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    I would split it up into chars, but that's just me.

    For example:
    - Read in the number as a whole string, not int by int, for this you can use something like scanf or fgets.
    - Then go through char by char, each char, subtract the char '0' - subtracting the char 0 (not the numerical value of 0) will convert each char into a number, you can simply tack that number onto a larger counter, ie. If the number is < 0 or > 9, then it's invalid.

    You know the string size by using a strlen() funct

    Code:
    int total_val = 0;
    int str_size = strlen(str_buff);
    int i;
    while (i = 0 ; i < str_size ; ++i)
       total_val += str_buff[i] - '0';
    The code to get it into string form may look something like.

    Now you need to check for bad input, so what you can do is simply:
    Code:
    int total_val = 0;
    int str_size = strlen(str_buff);
    int ch;
    int i;
    for (i = 0 ; i < str_size ; ++i)
    {
       ch = str_buff[i] - '0';
       if (ch < 0 || ch > 9)
       {
          printf("Invalid Entry\n");
          break;
       }
       total_val += ch;
    }
    That algorithm works, I tested it just now. Obviously you can set a boolean error flag or something so it doesn't print the total value upon error.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Hi guys thank for your help, but I am still currently undergoing the basic course for C so the string size we have yet to learn.

    What our assignment entails is to separate out the digit individually throught the division method. is there anyway to attempt this question from this angle? i have no idea how i can actually store unknown number of individual digits to an unknown number of variables to do the addition and subtraction!

    thanks for all your great help!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about using some logic like:

    This is logic which looks like code, but it is NOT code.

    Code:
    /* do the first round of summation of the digits.
    User is told to stop entering numbers by entering -1
    */
    while(1) { //any positive integer
      get user's number
      if(number < 0)
        break;
    
      sum+= number
    }
    
    /* now consolidate the sum, into one digit */
    while(sum > 9) {
      peel off the right most digit with % 10 and /10 method
      add that digit it to the sum
    }
    Avoids the whole "store unknown number of digits somewhere", problem.
    Last edited by Adak; 09-09-2010 at 09:30 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 08:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 08:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM

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