2

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.

  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – azurefrog Jan 31 '18 at 17:00
  • 1
    tl;dr - Just calling `recursion(remain, sum)` doesn't change the value of `sum`. – azurefrog Jan 31 '18 at 17:01

3 Answers3

2

Two things:

  1. You're ignoring the result of your recursive call.

Change

recursion(remain, sum); 

to

sum = recursion(remain, sum);
  1. In your base case, you ignore sum, which is the sum of the digits so far, and return just the last digit you worked on.

Change

return y;

to

return sum + y;
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Ahhhh Ok, that makes a lot of sense. Cant believe I missed that. I think in my head, I just skipped over that due to the return sum; – Java_Beginner Feb 01 '18 at 18:51
1

Here is code you can write which solves your problem :

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);
     System.out.println("The Sum of the digits is: "+ y);

 }   

public static int recursion(int y) {
  if(y/10>=1) {
      int tempvar =y%10;
      int remain=y/10;
      return tempvar + recursion(remain); 
  }
  else {            
      return y;
  }

}

Changes :

  1. You are ignoring the return value from recursive function .
  2. Terminating condition was not applied correctly .
  3. no need to pass current result in recursive function .

Please try the code given by me , i hope it will solve your problem .

Sahil Aggarwal
  • 1,311
  • 1
  • 12
  • 29
1

I wrote a program in C:

#include <stdio.h>

int power(int x, int y) //calculates x^y.
{
int product = 1;
for (int i = 0; i < y; i++)
{
    product = product * x;
}
return (product);
}

int num_digits(int x) //returns number of digit.
{
int z;
int i;
for (i = 0;; i++)
{
    z = x / power(10, i);
    if (z == 0)
        break;
}
return (i);
}

int first_digit(int x) //returns first digit.
{
int a = num_digits(x);
int b = power(10, a - 1);
int c = x / b;
return (c);
}

void del_first_digit(int *x) //deletes the first digit of number and restores 
                             //it at 
                             //original address.
{
int a = *x;
int b = first_digit(a);
int c = num_digits(a);
*x = (*x) - b * power(10, c - 1);
}

void main() //main()
{
int user_input;
printf("give the input::");
scanf("%d", &user_input);
int digits = num_digits(user_input);
int dump = 0;
for (int i = 1; i <= digits; i++)
{
    dump = dump + first_digit(user_input);
    del_first_digit(&user_input);
}
printf("SUM OF DIGITS OF THIS %d DIGIT NUMBER IS %d.",digits,dump);
}