WinnyPoh
WinnyPoh

Reputation: 11

Sum of first and last digit in recursion

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);

 }

Upvotes: 1

Views: 809

Answers (5)

WinnyPoh
WinnyPoh

Reputation: 11

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);
    }

Upvotes: 0

user15314575
user15314575

Reputation:

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);
}

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49920

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));
}

Upvotes: 4

Nexevis
Nexevis

Reputation: 4667

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.

Upvotes: 1

user3088799
user3088799

Reputation: 165

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);

Upvotes: 0

Related Questions