Question : Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print 4 2 3 3 9
Java Program to Extract Digits from A Given Integer
/*
* Filename: SeparatingDigits.java
*
* Description: Exercise 2.30 - Separating the Digits in an Integer
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;
public class SeparatingDigits{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] num = new int[5];
System.out.print("Enter a 5 digit number: ");
int x = input.nextInt();
// countdown to ensure numbers don't get reversed
for(int i=4; i>=0; --i){
num[i] = x % 10;
x /= 10;
}
for(int i : num){
System.out.printf("%d ", i);
}
System.out.println();
}
}