Advertisement
Nickpips

DigitRoots

Feb 4th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Root {
  4.  
  5. private static int sum( int a )
  6. {
  7. int ans = 0;
  8. while( a != 0 )
  9. {
  10. ans += a%10;
  11. a /= 10;
  12. }
  13. return ans;
  14. }
  15.  
  16. public static void main(String[] args) {
  17.  
  18. Scanner scan = new Scanner(System.in);
  19.  
  20. int N = scan.nextInt();
  21. while (N != 0) {
  22. while( N >= 10 )
  23. {
  24. N = sum(N);
  25. }
  26. System.out.println("Digital Root: " + N);
  27. N = scan.nextInt();
  28. }
  29.  
  30. scan.close();
  31.  
  32. }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement