8 Replies  648 Views  Last Post: 19 February 2013  06:23 PM
#1
enter a number and return an array that contains all of the digits
Posted 19 February 2013  10:01 AM
//I realy need help//
You must use % and /. 456 % 10 = 6 456 / 10 = 45
You must use % and /. 102 % 10 = 2 102 / 10 = 10
You must use % and /. 9123 % 10 = 3 9123 / 10 = 912
You must use % and /. 9 % 10 = 9 9 / 10 = 0
Sample Data :
567891
901912468
864213507
Sample Output :
1 5 6 7 8 9
0 1 1 2 4 6 8 9 9
0 1 2 3 4 5 6 7 8
Files Needed ::
NumberSort.java
Lab15e.java
Replies To: enter a number and return an array that contains all of the digits
#2
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  10:07 AM
digit[0]=int_number % 10
Then to get the next digit
int_number=(int_numberdigit[0])/ 10
all the way upto the digit[n]=0
then you'll have the digits in reverse order from digit[0]...digit[n1]
#3
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  10:28 AM
digit[0] = num % 10;
num /= 10;
/>
The tricky part is to determine the size of the int[] array
without using String
That should do it but sure there is a better way
int num = 213546; int size = 0; int numCopy = num; while(numCopy != 0) { ++size; numCopy /= 10; } int[] digit = new int[size]; for(int i = 0; i < size; ++i) { digit[i] = num % 10; num /= 10; } Arrays.sort(digit);
This post has been edited by pbl: 19 February 2013  12:24 PM
Reason for edit:: Yes = not  thanks to burakarltr
#4
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  10:34 AM
pbl, on 19 February 2013  12:28 PM, said:
digit[0]  num % 10;
num /= 10;
/>
Or perhaps
digit[0] = num % 10; num /= 10;
Quote
without using String
Use the log base 10 of n.
log base 10 1 = 0
log base 10 10 = 1
log base 10 100 = 2
etc.
#5
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  11:36 AM
Quote
without using String
That should do it but sure there is a better way
You could always do
int size = (int)Math.ceil(Math.log10(num)); size = (size == 0)? 1 : size;
Sorry  only just now noticed jon's comment
This post has been edited by g00se: 19 February 2013  11:43 AM
Reason for edit:: Clarification
#6
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  11:51 AM
ceil is going to fail on powers of 10.
int size = (int)Math.ceil(Math.log10(num));
gives 1 for 10, 2 for 100, etc. Better to do
int size = 1+ (int)(Math.log10(num));
#7
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  04:23 PM
http://stackoverflow...seperatedigits
http://stackoverflow...fanintnumber
http://www.coderanch...DigitsInteger
or our very own DIC archive: http://www.dreaminco...berintoparts/
This post has been edited by k3y: 19 February 2013  04:25 PM
#8
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  04:31 PM
Quote
#9
Re: enter a number and return an array that contains all of the digits
Posted 19 February 2013  06:23 PM
This post has been edited by jon.kiparsky: 19 February 2013  07:05 PM
