# Iterate Through Digits of a Number Java

Iterate Through Digits of a Number Java | In the section, we deal with the digits that we iterate over every digit of a number and extract the same. For example:-
Number:- 123
Result:- 1 2 3

We can iterate through digits of a number Java in the following ways,
1) By using the modulo operator (%)
2) By using the modulo operator & Math.pow()
3) By using Integer.toString() method

Usecase:- By iterating the digits of a number, you can find sum of digits of a number, sum of even digits in number, sum of odd digits in a number, sum of first & last digit of a number, sum of digits until single digit, finding number of occurrence of a particular digit in the given number (for example:- finding number of occurrence of 9 in the given number), and e.t.c.

## Iterate Through Digits of a Number Java Using Modulo Operator

We all know that the modulus operator returns the remained of the division. For example:- 123%10 gives 3, similarly 3245%100 gives 45.

``````import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scan.nextInt();

// if number is negative then make it positive
if (num < 0) {
num *= -1;
}

System.out.println("Digits in the given number are: ");
while (num != 0) {
int remainder = num % 10;
System.out.print(remainder + " ");
num = num / 10;
}

scan.close();
}
}``````

Output:-

Enter a number: 748465455
Digits in the given number are:
5 5 4 5 6 4 8 4 7

Enter a number: -123456789
Digits in the given number are:
9 8 7 6 5 4 3 2 1

Explanation of the code goes as follows:-
Step-1:- In the main class create the main method.
Step-2:- Initialize a variable with some number.
Step-3:- Use the while loop to iterate over the number, the loop is executed until the condition becomes false.
Step-4:- Then inside the while loop use the modulus operator and obtain the remainder of the number. When divided by 10 print the remainder, and again divide the number by 10. Repeat it for each and every digit in the given number.

The above program to iterate through digits of a number Java, traverse the digit in reverse order. For example if the number is 123456 then it traverse from right to left as 6, 5, 4, 3, 2, and 1. But if we want to traverse the digits from left to right the we can take help of Math.pow() method, division and modulo operators.

## Iterate Through Digits of a Number Java Using Math.pow() & Operators

Let us see an another program to iterate through digits of a number Java from left to right direction using Math.pow() method, division and modulo operators. The Math.pow() method returns the double value therefore we have to convert them into int value using typecasting.

``````import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scan.nextInt();

// if number is negative then make it positive
if (num < 0) {
num *= -1;
}

// count number of digits
int temp = num;
int totalDigits = 0;
while (temp != 0) {
totalDigits++;
temp /= 10;
}

// iterate through each digit of the number
System.out.println("Digits in the given number are: ");
for (int i = totalDigits - 1; i >= 0; i--) {
int digit = (num / (int) Math.pow(10, i)) % 10;
System.out.print(digit + " ");
}
scan.close();
}
}``````

Enter a number: 45158791
Digits in the given number are:
4 5 1 5 8 7 9 1

Enter a number: -15414454
Digits in the given number are:
1 5 4 1 4 4 5 4

## Java Program to Iterate through digits of a Number using String

We can do the same do the same with the help of String. For this, we have to convert given number into string. After that we will traverse the each character of the string. Later we will fetch the character at particular index using charAt() method and convert that character to int value.

In Java String class String.valueOf() method is given to convert all types of primitive values to string type. There are multiple overloaded forms of valueOf() method in String class.

``````import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scan.nextInt();

// if number is negative then make it positive
if (num < 0) {
num *= -1;
}

// convert number to string
String digits = String.valueOf(num);

System.out.println("Digits in the given number are: ");
for (int i = 0; i < digits.length(); i++) {
// find character at particular index and
// convert character to int value
int digit =
Integer.valueOf(String.valueOf(digits.charAt(i)));
System.out.print(digit + " ");
}
scan.close();
}
}``````

Output:-

Enter a number: 717485465
Digits in the given number are:
7 1 7 4 8 5 4 6 5

Enter a number: -1456174115
Digits in the given number are:
1 4 5 6 1 7 4 1 1 5

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!