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

27th Dec 2018, 3:36 PM
הראל ברודאי
הראל ברודאי - avatar
2 Respuestas
+ 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); } }
27th Dec 2018, 3:48 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Thanks
27th Dec 2018, 4:21 PM
הראל ברודאי
הראל ברודאי - avatar