Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package code;
- public class SuperDigitJava {
- //We define super digit of an integer x using the following rules:
- //If x has only 1 digit, then its super digit is x.
- //Otherwise, the super digit of x is equal to the super digit of the digit-sum of x.
- //Here, digit-sum of a number is defined as the sum of its digits.
- public static void main(String[] args) {
- //Integer x = 1;
- Integer x = 99;
- System.out.println(getSuperDigit(x));
- }
- public static Integer getSuperDigit(Integer x) {
- if(x < 10) {
- return x;
- } else {
- Integer digitSum = 0;
- for (char num : x.toString().toCharArray()) {
- digitSum += Character.getNumericValue(num);
- }
- return getSuperDigit(digitSum);
- }
- }
- }
Add Comment
Please, Sign In to add comment