0
What is wrong about this code?
I have wrote code that uses a Recursion what is wrong about it? Here is it https://code.sololearn.com/cGSFLoUmi8au/?ref=app
2 odpowiedzi
+ 14
● here 'num = num%10;' will never make num to 0, so resulting in StackOverflowError [Logical Error made in code]
//here is rough code (changes made by assuming U wish to add digits of number) :
public class Main {
public static int temp = 0;
public static void main(String[] args) {
System.out.println(sum(123));
}
public static int sum(int num){
if (num == 0){
return temp;
}
temp = temp + num % 10;
num = num / 10;
return sum(num);
}
}
+ 1
Thanks