Thread: Getting digits from int.

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Getting digits from int.

    Hello, Ive been trying to get the digits that exist in an integer, but I do not really know how to do that. SUppose I have:
    PHP Code:
    int i 100
    How can I put the different digits into separate variables? For example 1, 0, 0

    Thanks.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Use the modulus operator.

    eg.

    654 % 10 = 4
    65 % 10 = 5
    6 % 10 = 6

    int i = 654 % 10;

    i now contains 4

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Oh I see, helpful!

    I will try it. wait.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Ok, the problem with this, is that I never know the exact number at first. So I cannot really remove the last digit and try again using % 10.

    Do you have a work-around for that?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sure. enclose the entire process in a loop looking for a given value. The value is that the number you're working on remains greater than 9. After each modulus, decrease the initial input value by factor of 10. Store each modulus in an array for later use.

    Code:
    int input = //whatever
    int digitArray[12];
    int index = -1;
    
    while(input > 9)
    {
       digitArray[++index] = input % 10;
       input /= 10;
    }

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    #define MaxDigits 8
    
    void SplitDigits(int Number, int* DigitArray)
    {
       for(int i=(MaxDigits-1); i>=0; i++)
       {
          DigitArray[i] = Number % 10;
          Number /= 10;
       }
    }
    
    int main()
    {
       int DigitArray[MaxDigits];
       int Number = 1538;
    
       SplitDigits(Number, DigitArray);
    
       for(int i=0; i<MaxDigits; i++)
       {
          cout << DigitArray[i] << endl;
       }
    
       return 0;
    }
    Set a proper value for MaxDigits. In this case, the digits will be 0 0 0 0 1 5 3 8. If you only want the valuable digits (non-preceeding zeros) calculate MaxDigits using 10-logarithm (log10 ? I've never used it) but then you have to modify the arrays to be allocated dynamically.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Ok, will combine all your posts.

    Thank you so much.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Thanks a lot, it works.!!!!
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  9. #9
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    This fragment works for an any digit number (within long limits)

    void main()
    {
    long digit=12345;
    while(digit>0)
    {
    cout<<digit%10<<endl;
    digit/=10;
    }
    }

    Happy Hunting...
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM