
August 9th, 2006, 01:50 AM
#1
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.

August 9th, 2006, 02:21 AM
#2
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;
}

August 9th, 2006, 02:48 AM
#3
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,len1);
for (int i=len1;i>=0;i)
{
digits[leni1]=parseNo / dec;
parseNo = digits[leni1]*dec;
dec /= 10;
}

August 9th, 2006, 05:16 AM
#4
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.

August 9th, 2006, 09:59 AM
#5
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.

August 9th, 2006, 12:35 PM
#6
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.

June 3rd, 2013, 05:00 PM
#7
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

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
