
August 9th, 2006, 12: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, 01: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, 01: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, 04: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, 08: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, 11:35 AM
#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.

August 9th, 2006, 06:15 PM
#7
Re: Getting the individual digits of a number?
Thanks everyone, for the reply!

August 10th, 2006, 02:21 AM
#8
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[leni1]=parseNo % 10;
parseNo /= 10;
}

June 1st, 2013, 04:02 AM
#9
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;
}

June 3rd, 2013, 04:00 PM
#10
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 survey question.
Featured
