Recursive Digit Sum

  • + 1 comment

    My code seems to work in Eclipse but it turns wrong in the test cases

    public static int superDigit(String n, int k) {
    	    // Write your code here
    	        BigInteger superDigit = new BigInteger(n);
    	        StringBuilder sb = new StringBuilder();
    	        for (int i = 0; i < k; i++) {
    	            sb.append(n);
    	        }
    	        n = sb.toString();
    	        char[] superDigitCharArray;
    	        while(superDigit.compareTo(new BigInteger("10")) != -1){
    	            int sum = 0;
    	            superDigitCharArray = n.toCharArray();
    	            for (char c : superDigitCharArray) {
    	                sum += Character.getNumericValue(c); 
    	            }
    	            superDigit = new BigInteger(sum + "");
    	            n = superDigit + "";
    	        }
    	        
    
    	        return superDigit.intValue();
    	    }