We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static int superDigit(String n, int k) {
if(n.length()==1)
return Integer.parseInt(n);
long sum = 0;
int i =0;
int[] digitArr = new int[n.length()];
while(i<n.length()){
digitArr[i]=Integer.parseInt(n.charAt(i)+"");
i++;
}
for(int j=0; j<digitArr.length;j++){
if(digitArr[j]!=0)
sum+=digitArr[j]*k;
}
//Calling the function recursively with n = String value of sum
// and k = 1 as there is no repition required
return superDigit(String.valueOf(sum), 1);
}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
`//Java solution with recursion:-
public static int superDigit(String n, int k) { if(n.length()==1) return Integer.parseInt(n);
`