CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Mar 2006

    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.
    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].
    /* 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
    3311'18.10"N 9645'20.28"W

    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.

    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

    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
    Craiova, Romania

    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 :

    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;

    Hope it's useful.

  5. #5
    Join Date
    Mar 2004
    3311'18.10"N 9645'20.28"W

    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


    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
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    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.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.