+ 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