CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2006
    Posts
    7

    Question Getting the individual digits of a number?

    I'm having trouble getting the individual digits of a number.

    For example:
    say you have simple integer variable containing 1234. How do you get the individual digits of the number, like 1, 2, 3, and 4 individually?

    It should be somthing like this.
    Code:
    int number = 1234;
    
    get the digits()
    {
    	int[4] digits;
    	digits[0] = the first digit of [int number].
    	digits[1] = the second digit of [int number].
    	digits[2] = the third digit of [int number].
    	etc...
    }
    
    /* the end result should be:
    
    digits[0] = 1;
    digits[1] = 2;
    digits[2] = 3;
    digits[3] = 4;
    
    */
    Is this possible?

    Thanks in advanced.

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Getting the individual digits of a number?

    you can do it a couple of ways. the easiest (to me atleast) is just to take the string value, then return an array of the characters that make up the number. you can also create an array by doing bit manipulation.

    Code:
    public int[] GetDigits(int number) {
        string temp = number.ToString();
        int[] rtn = new int[temp.Length];
        for(int i = 0; i < rtn.Length; i++) {
            rtn[i] = int.Parse(temp[i]);
        }
        return rtn;
    }

  3. #3
    Join Date
    Apr 2005
    Posts
    576

    Re: Getting the individual digits of a number?

    MadHatter's way is the easiest.

    Doing it without strings would be something like:

    PHP Code:
    int no=1234;

    int len=(int)Math.Ceiling(Math.Log10(no));

    int[] digits=new int[len];

    int parseNo=no;
    int dec=(int)Math.Pow(10,len-1);

    for (
    int i=len-1;i>=0;--i)
    {
        
    digits[len-i-1]=parseNo dec;
        
    parseNo -= digits[len-i-1]*dec;
        
    dec /= 10;


  4. #4
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    Re: Getting the individual digits of a number?

    I think you are a little confused from the methods above.
    The simplest way is the math way :

    Code:
    int digit;
    int no=1234;
    int res = no;   //we copy the number no to res because we don't want to change no
    
    while (res!=0)  //while res is != from 0 we take a digit
    {
    digit = res % 10;    //You get the last digit 
    //Here do what you want with your digit
    res \= 10;     // You take out the last digit from the number
    }
    Here is the result for no =1234;
    res =1234;
    digit = 4;
    res = 123;
    .....
    digit =1;
    res = 0;
    Stop.

    Hope it's useful.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Getting the individual digits of a number?

    if you do it that way then you either have to use an arraylist then ToArray it, or you have to resize an int[] on every pass.

  6. #6
    Join Date
    Jan 2005
    Posts
    56

    Thanks

    Being Old School, my first solution would have been the same as creatorul but in my heart I knew there was a better way in C#. Thank you MadHatter and klintan for teaching an old dog a couple new tricks. This is why I read this daily.

  7. #7
    Join Date
    Mar 2006
    Posts
    7

    Re: Getting the individual digits of a number?

    Thanks everyone, for the reply!

  8. #8
    Join Date
    Apr 2005
    Posts
    576

    Re: Getting the individual digits of a number?

    I think creatorul's code was the nicest (even though I probably would have used strings), if you just first determine number of digits you will not need to resize the array:

    PHP Code:
    int no=1234
    int len=(int)Math.Ceiling(Math.Log10(no)); 
    int[] digits=new int[len]; 
    int parseNo=no
    for (
    int i=0;i<len;++i

        
    digits[len-i-1]=parseNo 10
        
    parseNo /= 10


  9. #9
    Join Date
    Jun 2013
    Posts
    1

    Smile Re: Getting the individual digits of a number?

    #include<iostream>
    #include<conio.h>
    using namespace std;

    int main()
    {
    int l,n,a,sum=0,i=1;

    cout<<"\nEnter number length:";
    cin>>l;
    int x[l];
    cout<<"\nEnter number:";
    cin>>n;

    cout<<"\nindividual digits are:";
    while(n!=0)
    {
    a=n%10;
    x[i]=a;
    n=n/10;
    sum+=a;
    i++;
    }


    for(i=l;i>=1;i--)
    {
    cout<<" "<<x[i];
    }
    cout<<"\n\nsum is:"<<sum;
    getch();
    return 0;




    }

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Getting the individual digits of a number?

    @nawalmir, you've just responded to a 7 year old post with an invalid solution. Your solution is a C++ solution and this is a C# forum.

    Besides that, welcome to the site.

  11. #11
    Join Date
    Nov 2020
    Posts
    1

    Re: Getting the individual digits of a number?

    This is very weird seeing this thread 16 years later. cool tho.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured