Pretty new to Java but I'm working on a tutorial where I have to find the Sum of Digits of a user input integer using recursion. Here is my code so far:
public class Others {
public static void main(String[] arg) {
Scanner s=new Scanner(System.in);
System.out.println("Enter any integer: ");
int sum=0;
int x=s.nextInt();
int y=recursion(x, sum);
System.out.println("The Sum of the digits is: "+ y);
}
public static int recursion(int y, int sum) {
if(y/10>=1) {
int tempvar =y%10;
int remain=y/10;
sum+=tempvar;
if(remain!=0) {
recursion(remain, sum);
}
return sum;
}
else {
return y;
}
}
So if I put input: 123, it returns 3. I went through this program on paper step by step and logically I cant think of anything that I missed.