If I have a number 2500, I want the output to be: 2, 5, 0, 0. How can I separate the numbers into digits?
May 3, 2018 in Java 193,441 views

## 6 answers to this question.

We can easily separate the digits of an integer number using Java.

```public static void main(String[] args) {

int num=1020; // int number

while (num > 0) {

System.out.println( num % 10);

num = num / 10;

}

}```
• 3,190 points
Sir This One Is showing Reverse Of Input Numbers

Yes @Faizan Ali this will give you the numbers in reverse order. You will need to push them onto a stack and pop them off in reverse order.

Code to print the numbers in the correct order:

```int number; // = and int
while (number > 0) {
stack.push( number % 10 );
number = number / 10;
}

while (!stack.isEmpty()) {
print(stack.pop());
}```

Hope it works now!!

Thank you!!

You can convert a number into String and then you can use toCharArray()  or split() method to separate the number into digits.

```String number = String.valueOf(someInt);

char[] digits1 = number.toCharArray();
// or:
String[] digits2 = number.split("(?<=.)");```

Still unable to separate the digits, enroll now with one of the best Java certification course offered by Edureka.

• 6,910 points
import java.util.*;

public class AmstrongNumber {

public static void main(String args[])

{

int a,i,count=0;

Scanner s= new Scanner(System.in);

System.out.println("Enter a number");

a=s.nextInt();

while (a > 0) {

i= a % 10;

System.out.println(i);

a = a / 10;

}

//System.out.println(a);

}

}
answered Aug 6, 2019 by anonymous
This will print the values in reverse order not the orignal one.
you saved my life bro

Hey, Guys

I have found a new solution to this existing is:

toCharArray() to Get the Array of Characters. Another way of separating the digits from an int number is using the toCharArray() method. We will convert the integer number into a string and then use the string's toCharArray() to get the characters' array. Now we can print out all the characters one by one.

• 65,930 points

Hello guys,

The javaScript split method can be used to split numbers into arrays. But the split method only splits string, so first, you need to convert Number to String. Then you can use the split method with a combination of map method to transform each letter into a Number.

• 8,910 points

You can also have a look here:

To do this, you will use the % (mod) operator.

```int number; // = some int

while (number > 0) {
print( number % 10);
number = number / 10;
}
```

The mod operator will give you the remainder of doing int division on a number.

So,

```10012 % 10 = 2
```

Because:

```10012 / 10 = 1001, remainder 2
```

Note: this will give you the numbers in reverse order. You will need to push them onto a stack and pop them off in reverse order.

Code to print the numbers in the correct order:

```int number; // = and int
while (number > 0) {
stack.push( number % 10 );
number = number / 10;
}

while (!stack.isEmpty()) {
print(stack.pop());
}```
• 10,520 points

## What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

## How can I find the prime factors of an integer in JavaScript?

Using a for loop in javascript, I was attempting to determine the prime factors of a number, denoted as 'integer' below.  I can't seem to get it to work, and I'm not sure if it's because of my JavaScript or my calculating logic. //integer is the value for which we ...READ MORE

## How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

## How can I convert an 'ArrayList<String> to 'String[]' in Java

In Java 8 or later: String listString = ...READ MORE

## How can I convert a String variable to a primitive int in Java

Here are two ways illustrating this: Integer x ...READ MORE

## How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

## Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

## String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE