1

I had a test exam in java, that almost no one have succeeded in this question and I can't figure out the solution.

The question was like this:

Find the sum of an integer last and first number. For example 1234-->5, 137-->8, 4-->8. You are only allowed to use recursion and no helper function"

I tried various things. Here is my last attempt:

public static int sumOfFirstandLastdigits(int number)
 {
    int lastdigit=sumOfFirstandLastdigits(number/10);
    if(number/10==0)
      {
           return number%10;
      }
     return lastdigit+sumOfFirstandLastdigits(number%10);

 }
Amal K
  • 3,630
  • 2
  • 16
  • 38
WinnyPoh
  • 11
  • 1

6 Answers6

4

Assuming the input is supposed to be non-negative:

//If n < 0, return first digit of -n
//Otherwise, return sum of first and last digits of n
int sumLastAndFirstDigit(int n) {
    if (n < -9)
        return sumLastAndFirstDigit(-(-n/10));
    if (n <= 0)
        return -n;
    if (n < 10)
        return n+n;
    return n%10 + sumLastAndFirstDigit(-(n/10));
}
Scott Hunter
  • 46,905
  • 10
  • 55
  • 92
1

You can do this by overloading the method and passing the last digit as a second parameter to keep track of it through the recursion without changing the value (AKA Default Parameter):

public static void main(String[] args) {
    
    System.out.println(sumDigits(3891901));
    System.out.println(sumDigits(1234));
    System.out.println(sumDigits(5678));

}

private static int sumDigits(int i) {
    return sumDigits(i, i % 10);
}

private static int sumDigits(int i, int j) {
    if (i / 10 == 0) {
        return i % 10 + j;
    }
    return sumDigits(i / 10, j);
}

Output:

4
5
13

This thread on default parameters might help learn more as well.

Nexevis
  • 4,383
  • 3
  • 12
  • 21
0

Found a solution using String, not sure it's the best :

    public int sumLastAndFirstDigit(Integer firstDigit, int number) {
        String numberAsString = String.valueOf(number);
        //Set the first digit
        if(firstDigit == null) {
            firstDigit = Integer.valueOf(numberAsString.substring(0,1));
            //If there is only one digit in number for the first loop, then return 2 x firstDigit
            if(numberAsString.length() == 1) {
                return 2 * firstDigit;
            }
        }
        //Remove the first digit to create the new number
        String newNumberAsString = numberAsString.substring(1);
        Integer newNumber = Integer.valueOf(newNumberAsString);
        if(newNumberAsString.length() == 1) {
            //When it's the last digit, sum first and last
            return firstDigit + newNumber;
        }
        return sumLastAndFirstDigit(firstDigit, newNumber);
    }

Then do :

sumLastAndFirstDigit(null,1234);
sumLastAndFirstDigit(null,137);
sumLastAndFirstDigit(null,4);
user3088799
  • 162
  • 6
0

What i would do is to add a second parameter, which states if it is the first call of the recursive method.

public static int sumOfFirstandLastdigits(int number, boolean isFirstCall)
{
  int last = 0; //stays 0 if it is not the first call

  if(isFirstCall) {
    last = number%10;
  }

  if(number/10 == 0) {
    return last + number;
  } else {
    return last + sumOfFirstandLastdigits(number/10, false);
  }
}

An overloaded method for the first call then could look like this:

public static int sumOfFirstandLastdigits(int number) {
  sumOfFirstandLastdigits(number, true)
}
csalmhof
  • 1,679
  • 2
  • 15
  • 23
0

Use the sign as a flag to recognize the initial call. Only works with positive numbers, of course.

public static int sum(int value){ 
    if(value > 0)
        // initial call
        return value % 10 + sum(-value);
    else
        // recursive call
        return -value < 10 ? -value : sum(value / 10);
}
QuinncyJones
  • 539
  • 1
  • 4
  • 10
0

This are the 2 different solutions my professor suggested, although he said no helper (the first one is without an helper).

public static int sumFirstAndLast2(int num) {
        if (num < 10 )
            return num+num;
        return sumFirstAndLast2(num/10) - num%100/10 + num%10;
    }
    
    public static int sumFirstAndLast(int num) {
        if ( num < 10 )
            return num+num;
        return sumFirstAndLastHelper(num,true);
    }
    private static int sumFirstAndLastHelper(int num, boolean isLast) {
        if ( isLast )
            return num%10 + sumFirstAndLastHelper(num/10,false);
        if ( num < 10 )
            return num;
        return sumFirstAndLastHelper(num/10,false);
    }
csalmhof
  • 1,679
  • 2
  • 15
  • 23
WinnyPoh
  • 11
  • 1