**Problem Statement:** For any random number taken into consideration the goal is to access every digit of the number.

**Illustration: **Simply taking two numbers be it- 12345 and 110102 absolutely random pick over which computation takes place as illustrated:

Input:12345Output:1 // 1 digit 2 // Digit next to 1st digit 3 4 5 // Last Digit of the numberInput:110102Output:1 1 0 1 0 2

Now, In order to access the digits of a number, there are several approaches ranging from the brute-force approach to the most optimal approach. Two standard approaches are as follows:

**Approaches:**

- Using Integer.toString Function (Using inbuilt Method)
- Using modulo operator

**Approach 1: Using**** Integer.toString ****Method**

This method is an inbuilt method present already in the directory of java which returns the string object. this string object is representing the integer value.

**Syntax:** As it is awarded that Strings are immutable in Java meaning String is a class in java.** **So,

public static String toString()

The directory in which it is present is as follows:

java.lang.Integer.toString()

**Return Method:** As seen above in the syntax itself this method returns the string object of the integer value over which it is applied. Also

In this approach we will convert the integer into a string then we will traverse the string.

- Take the integer input.
- Convert the input into string data.
- Traverse through the string and print the character at each position that is the digits of the number.

Below is the implementation of the above approach:

## Java

`// Java program to Extract Digits from A Given Integer` `// Importing Libraries` `import` `java.util.*;` `import` `java.io.*;` `class` `GFG {` ` ` `// Main driver function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// Declaring the number` ` ` `int` `number = ` `110102` `;` ` ` `// Converting the integer input to string data` ` ` `String string_number = Integer.toString(number);` ` ` `// Traversing through the string using for loop` ` ` `for` `(` `int` `i = ` `0` `; i < string_number.length(); i++) {` ` ` `// Printing the characters at each position` ` ` `System.out.println(string_number.charAt(i));` ` ` `}` ` ` `}` `}` |

*chevron_right*

*filter_none*

**Output:**

1 1 0 1 0 2

**Approach 2: **Using Modulo Operator

Without using inbuilt methods** **whenever it comes down to extraction of digit straightaway modulo with 10 is the answer because it extracts out the last digit. similarly, if the process is carried for the number obtained after removing the digit that is already extracted will extract out the last digit from the small number. The same process is iterated till the end is extracted. So,

In this approach**,** the last digit from the number and then remove it and is carried on till reaching the last digit.

- Take the integer input.
- Finding the last digit of the number.
- Print the last digit obtained and then remove it from the number.
- Keep on following the step 2 and 3 till we reach the last digit.

Below is the implementation of the above approach:

## Java

`// Java program to Extract Digits from A Given Integer` `// Importing Libraries` `import` `java.util.*;` `import` `java.io.*;` `class` `GFG {` ` ` `// Main driver function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// Declaring the number` ` ` `int` `number = ` `110102` `;` ` ` `// Printing the last digit of the number` ` ` `while` `(number > ` `0` `) {` ` ` `// Finding the remainder (Last Digit)` ` ` `int` `remainder = number % ` `10` `;` ` ` `// Printing the remainder/current last digit` ` ` `System.out.println(remainder);` ` ` `// Removing the last digit/current last digit` ` ` `number = number / ` `10` `;` ` ` `}` ` ` `}` `}` |

*chevron_right*

*filter_none*

**Output:**

2 0 1 0 1 1

Attention reader! Don’t stop learning now. Get hold of all the important **Java Foundation** and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.