-3

Given a non-negative integer, I need to return an array containing the digits of the initial number, considering that the initial number shall be less than 100,000. For example:

separateDigits(93) → {9, 3}
separateDigits(125) → {1, 2, 5}
separateDigits(732) → {7, 3, 2}

How do I separate the digits?

user5183901
  • 159
  • 1
  • 1
  • 6

2 Answers2

0

You need to create an array but first you need to know how big. Then you need to fill the array with digits.

public byte[] separateDigits(long n) {
    int digits = 1; 
    for(long m = n; (m /= 10) > 0; digits++);

    byte[] ret = new byte[digits];
    for(int i = digits - 1 ; i >= 0; i--, n /= 10)
        ret[i] = (byte) (n % 10);
    return ret;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
//this might help you

import java.util.*;
public class SeparaeDigits {

    public static void main(String[] args) {
        int num =123;
     seperateDigits(num);


    }
    public static void seperateDigits(int num){
        int i=0;
        int remainder =0;
        int []arr = new int[3];
while(num>0){
 remainder = num % 10;
num = num/10;
for(i =0; i<arr.length; i++){
    arr[i]=remainder;

}
for(i =0; i<arr.length; i++){
System.out.println(arr[i]);
}

}
}
}