Guest User

Untitled

a guest
Mar 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. package code;
  2.  
  3. public class SuperDigitJava {
  4.  
  5.  
  6. //We define super digit of an integer x using the following rules:
  7. //If x has only 1 digit, then its super digit is x.
  8. //Otherwise, the super digit of x is equal to the super digit of the digit-sum of x.
  9. //Here, digit-sum of a number is defined as the sum of its digits.
  10. public static void main(String[] args) {
  11. //Integer x = 1;
  12. Integer x = 99;
  13. System.out.println(getSuperDigit(x));
  14.  
  15. }
  16.  
  17. public static Integer getSuperDigit(Integer x) {
  18. if(x < 10) {
  19. return x;
  20. } else {
  21. Integer digitSum = 0;
  22. for (char num : x.toString().toCharArray()) {
  23. digitSum += Character.getNumericValue(num);
  24. }
  25. return getSuperDigit(digitSum);
  26. }
  27. }
  28.  
  29. }
Add Comment
Please, Sign In to add comment