add one integer with is own number

This is a discussion on add one integer with is own number within the C Programming forums, part of the General Programming Boards category; Anyone have an easy way to do that Code: int number = 18; int number2 = 2; int addnumber; //I ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    add one integer with is own number

    Anyone have an easy way to do that
    Code:
    int number  = 18;
    int number2 = 2;
    int addnumber;
    
    //I want to add 1 + 8 + 2 for example so that int addnumber = 11
    by the way, it's not sure that the number will be over 10 so there is surely a if
    thanks once again
    Last edited by nevrax; 03-29-2007 at 09:41 PM.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I'm not sure your math background but you could check the number of digits with log(|number|). As far as solving your overall problem, I think the easiest(most straightforward) way would be to convert the numbers to a string and then add each character of the string to the total. You can do that or do some pure math with something like:
    Code:
    // this will add all the digits in number
    int digit_sum = 0;
    for (int i = 0; i <= (int)log(abs(number)); i++)
    	digit_sum += (int)(abs(number) / pow(10, i)) &#37; 10;
    Code:
    (int)log(abs(number))
    this returns the number of digits in the number
    Code:
    (int)(abs(number) / pow(10, i)) % 10)) % 10
    This singles out the ith digit. For example, in the number 486, 6 is the 0th digit, 8 is the 1st digit, 4 is the 2nd digit.
    Don't quote me on that... ...seriously

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    mmmm....something like this should work:
    Code:
    num=0x000345
    int digit,digit_total;
    //get rid of the leading zeros
    int i =0;
    while(!(digit=(num<<i)&(1<<sizeof(int))) && i < sizeof(int))
       i++;
    
    //now count
    digit_total = 0
    while ( i < sizeof(int) )
    {
         digit = ( num << i ) & ( 1 <<sizeof(int));
         digit_total +=digit;
    }
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no need to check the number of digits.
    Code:
    int sumdig( int d )
    {
        int x = 0;
        while( d )
        {
            x += d &#37; 10;
            d /= 10;
        }
        return x;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    oops !. Disregard my previous reply. I was thinking about binary number
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quzah's code also works for any base >= 2 by just replacing 10 with that base, so the function could take the base as an extra argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM

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