0

I have to write a recursive method to sum the figures of an integer.

The method works fine, but I don't understand why it returns the last integer instead of the total.

import java.util.Scanner;

public class SommaCifreRicorsivo{
    public static void main (String[] args){
        System.out.printf("Inserire un numero: ");
        Scanner tastiera = new Scanner(System.in);
        int numero = tastiera.nextInt();
        int somma = 0;

        int risultato = eseguiSomma(numero,somma);
        System.out.println("La somma delle sue cifre è " + risultato);
    }

    public static int eseguiSomma(int numero, int somma){
        //Caso base
        if (numero < 10) {
            somma = somma + numero;
            System.out.println("Aggiungo la cifra " + numero + " alla somma, ottenendo " + somma);
            return (somma += numero);
        }

        //Chiamate ricorsive
        somma = somma + numero%10;
        System.out.println("Aggiungo la cifra " + (numero%10) + " alla somma, ottenendo " + somma);
        eseguiSomma((numero/10), somma);

        return somma;

    }
} 
0

2 Answers 2

1

somma is a fundamental type (int), so Java passes it by value. This means the value you are modifying is just a copy of somma stored on the stack frame of each recursive call, not the final return value.

Because of this, the argument somma is entirely unnecessary. You should instead return the return value of the recursive call plus the new digit. This gives a much simpler solution.

public static int eseguiSomma(int numero){
    //Caso base
    if (numero < 10) {
        return numero;
    }

    //Chiamate ricorsive
    return numero % 10 + eseguiSomma(numero / 10);
}

// now simply call without somma
int risultato = eseguiSomma(numero);
Sign up to request clarification or add additional context in comments.

Comments

0

Simply correct it to :

import java.util.Scanner;

public class SommaCifreRicorsivo{
    public static void main (String[] args){
        System.out.printf("Inserire un numero: ");
        Scanner tastiera = new Scanner(System.in);
        int numero = tastiera.nextInt();
        int somma = 0;

        int risultato = eseguiSomma(numero,somma);
        System.out.println("La somma delle sue cifre è " + risultato);
    }

    public static int eseguiSomma(int numero, int somma){
        //Caso base
        if (numero < 10) {
            somma = somma + numero;
            System.out.println("Aggiungo la cifra " + numero + " alla somma, ottenendo " + somma);
            return (somma += numero);
        }

        //Chiamate ricorsive
        somma = somma + numero%10;
        System.out.println("Aggiungo la cifra " + (numero%10) + " alla somma, ottenendo " + somma);

        // the error was on this line!
        somma += eseguiSomma((numero/10), somma);

        return somma;

    }
} 

You are trying to modify somma, but somma is a raw type so you only modify its local copy and somma remains unchanged.

1 Comment

Yes sorry, I did write = instead of +=, correcting my answer right away, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.