# enter a number and return an array that contains all of the digits

Page 1 of 1

## 8 Replies - 776 Views - Last Post: 19 February 2013 - 06:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312729&amp;s=48796455df4350e6d1452a04997dcfa2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Trent4296

Reputation: 0
• Posts: 4
• Joined: 19-February 13

# enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:01 AM

Lab Description : Java Send in a number and get back an array that contains all of the digits from the number in sorted order. 634152 would come back [1, 2, 3, 4, 5, 6]. You may not use Strings in this program.
//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

Is This A Good Question/Topic? 0

## Replies To: enter a number and return an array that contains all of the digits

### #2 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:07 AM

Use the fact that the reversal digit by digit evaluation of an integer requires

digit[0]=int_number % 10

Then to get the next digit

int_number=(int_number-digit[0])/ 10

all the way upto the digit[n]=0

then you'll have the digits in reverse order from digit[0]...digit[n-1]

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:28 AM

I think you mean

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 jon.kiparsky

• Pancakes!

Reputation: 8933
• Posts: 15,439
• Joined: 19-March 11

## 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:

I think you mean

digit[0] - num % 10;
num /= 10;

/>

Or perhaps
```digit[0] = num % 10;
num /= 10;

```

Quote

The tricky part is to determine the size of the int[] array
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 g00se

• D.I.C Lover

Reputation: 3104
• Posts: 13,388
• Joined: 20-September 08

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 11:36 AM

Quote

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

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 jon.kiparsky

• Pancakes!

Reputation: 8933
• Posts: 15,439
• Joined: 19-March 11

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 11:51 AM

It's a rare day when I get to catch someone out on a math detail.

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 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 04:23 PM

How about these for some more references?:
http://stackoverflow...seperate-digits
http://stackoverflow...f-an-int-number
http://www.coderanch...-Digits-Integer

or our very own DIC archive: http://www.dreaminco...ber-into-parts/

This post has been edited by k3y: 19 February 2013 - 04:25 PM

### #8 g00se

• D.I.C Lover

Reputation: 3104
• Posts: 13,388
• Joined: 20-September 08

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 04:31 PM

Quote

ceil is going to fail on powers of 10.
Oops - yes

### #9 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 06:23 PM

You Can Find an Exemplary Implementation here .

Spoiler

This post has been edited by jon.kiparsky: 19 February 2013 - 07:05 PM