Java: Breaks an integer into a sequence of individual digits
Break Integer into Digits
Write a Java program to break an integer into a sequence of digits.
An integer is a number that can be written without a fractional component. For example, 12, 8, 0, and -2365 are integers, while 4.25, 57 1 / 2, and √3 are not
Test Data
Input six non-negative digits: 123456

Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise11 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input six non-negative digits: ");
int input = in.nextInt();
int n1 = input / 100000 % 10;
int n2 = input / 10000 % 10;
int n3 = input / 1000 % 10;
int n4 = input / 100 % 10;
int n5 = input / 10 % 10;
int n6 = input % 10;
System.out.println(n1 + " " + n2 + " " + n3 + " " + n4 + " " + n5 + " " + n6);
}
}
Sample Output:
Input six non-negative digits: 123456 1 2 3 4 5 6
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Java program to break an integer into digits and then reconstruct the number in reverse order.
- Write a Java program to extract digits from an integer and then calculate the frequency of each digit.
- Write a Java program to break an integer into digits without converting it to a string, using arithmetic operations only.
- Write a Java program to break an integer into its digits and then compute the sum and product of these digits simultaneously.
Go to:
PREV : Arithmetic Operations on Two Integers.
NEXT : Check Finite Floating-Point Value.
Java Code Editor:
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.