This is a Java Program to Take a Number and Return List of its Digits.

Enter any integer number as an input. After that we count the number of digits in the given input. The given number along with length of that number is passed to the other function where by using modulus and division operation we get all the digits of the given number.

Here is the source code of the Java Program to Take a Number and Return List of its Digits. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;

public class Digits_Number

`{`

int[] digits(int m,int l)

`{`

int x[] = new int[l];

int i = 0;

while(m > 0)

`{`

x[i] = m % 10;

m = m / 10;

`i++;`

`}`

return x;

`}`

public static void main(String[] args)

`{`

int counter = 0, n, m;

Scanner s = new Scanner(System.in);

System.out.print("Enter any number:");

n = s.nextInt();

m = n;

while(n > 0)

`{`

n =n / 10;

`counter++;`

`}`

Digits_Number obj=new Digits_Number();

int a[]=obj.digits(m, counter);

System.out.print("Digits:");

for(int i = a.length-1; i >= 0; i--)

`{`

System.out.print(a[i]+" ");

`}`

`}`

`}`

Output:

$ javac Digits_Number.java $ java Digits_Number Enter any number:6789 Digits:6 7 8 9

**Sanfoundry Global Education & Learning Series – 1000 Java Programs.**

advertisement

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Practice Programming MCQs
- Practice Information Technology MCQs
- Check Programming Books
- Apply for Java Internship
- Practice BCA MCQs