-1

Example:

Input:

140

Output:

1 4 0

I wanted to make it divide by 100 so that the outcome will be 1, and then divide by 10 and the outcome will be 4 and then by 1 and the answer will be 0. But I am not sure how I am able to achieve it. I also want to use recursion in the method.

moha
  • 579
  • 2
  • 9
  • 27
Thirty One
  • 31
  • 1

1 Answers1

1

You can see an int as a String

int n = 140;
String s = String.valueOf(n);
for(int i = 0; i<s.length(); i++){
    System.out.println(s.charAt(i));
}

With no need of recursion.

With recursion it could be something like (i haven't tried it so it could not work):

public String separateInteger(int n){
   if(n < 10){
       return String.valueOf(n);
   }
   else{ 
       int mod = n%10;
       int quot = n/10;
       return String.valueOf(mod) + separateInteger(quot);
   }
}

I hope have answered your question. :)

Cristian
  • 36
  • 5