Yay recursion!
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11332 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (true) { long x = sc.nextLong(); if (x == 0) break; out.println(solve(x)); } out.close(); sc.close(); } private static long solve(long x) { if (x < 10) return x; long sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return solve(sum); } }