// Read in a positive integer and sum up its digits to reduce it to a // new integer, repeating the process until only a single digit // remains. // // Example: user enters 8974 // 8974 reduces to 28 which reduces to 10 which reduces to 1 public class ReduceInteger { public static void main(String[] args) { int number, sum; // read in the number to reduce // assume the input is positive TokenReader in = new TokenReader(System.in); System.out.println("Enter a number to be reduced:"); number = in.readInt(); // leave a blank line and repeat the starting value System.out.println(); System.out.println("Starting value: " + number); // reduce the current number until it is a single digit while (number > 9) { sum = 0; // add up the digits of the current number // assume sum == 0 to start while (number > 9) { sum = sum + (number % 10); number = number / 10; } sum = sum + number; number = sum; System.out.println("Reduces to: " + number); } System.out.println("Done!"); } }