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.
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;
}
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;
}
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.
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.
Re: Getting the individual digits of a number?
Thanks everyone, for the reply! :)
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;
}
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;
}
:):wave:
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.
Re: Getting the individual digits of a number?
This is very weird seeing this thread 16 years later. cool tho.