Doubt about recursion in C

1

I'm learning about recursion, using the C language, and I have to do the following exercise:

  

Design a recursive function that takes an integer n and computes the sum of the digits of n.

     

For example:

     

for n = 327 , the result is 12 = 3 + 2 + 7 .

Until I have the resolution of this problem, it would be the following code:

 int soma(int n){
 if(n%10 == n)
 return n;
 else
 return ((n % 10) + soma(n/10));
 }

But I did not exactly understand the logic of this code, so I thank anyone who can explain me =]

    
asked by anonymous 27.03.2018 / 02:46

3 answers

5

Changing only the call order, but keeping the same sense:

 return (soma(n/10) + (n % 10));

Each time you call the same function again, you are passing the number without the first right digit , which happens when you perform n/10 (note that the division only returns the part of the division ). This process is repeated until n%10 == n (which in this case occurs on the 3rd Call ). When the termination condition is found on the third call (that is, n == 3 and n%10 == 3 , then n%10 == n ), the function begins to return the remainder of the division, strong> first digit right n%10 , added to the rest of the function in which you called it. with this you are adding all the remainders of the division, or, all the digits of the number.

Ifweexpandonarecursionnotation,wewouldhave:

soma(327)soma(32)+7soma(3)+2+73+2+7

Orinbinarytreenotation

    
27.03.2018 / 16:37
2

Code:

#include <iostream>

int soma(int n){

 if(n%10 == n)
    return n;
 else
    return ((n % 10) + soma(n/10));

}

int main(int argc, char** argv) {
    printf("%d\n", soma(327));
    return 0;
}

Result:

Explanation:

Stepbystepofthefunctionsoma()passingasaparameterthevalue327.

Step1:

intsoma(intn){//n=327if(n%10==n)//Orestodadivisãode327por10éiguala327?não,entãovamosparaoelse..returnn;elsereturn((n%10)+soma(n/10));//Retornaorestodadivisãode327por10maisoretornodafunção//somapassandocomoparâmetrooresultadointeirodadivisãode327//por10,ouseja,retorna7maisoretornodesoma(32).}

Step2:

intsoma(intn){//n=32if(n%10==n)//Orestodadivisãode32por10éiguala32?não,entãovamosparaoelse..returnn;elsereturn((n%10)+soma(n/10));//Retornaorestodadivisãode32por10maisoretornodafunção//somapassandocomoparâmetrooresultadointeirodadivisãode32//por10,ouseja,retorna2maisoretornodesoma(3).}

Step3:

intsoma(intn){//n=3if(n%10==n)//Orestodadivisãode3por10éiguala3?Sim!!returnn;//Entãoretorna3elsereturn((n%10)+soma(n/10));}

Step4:

//Somarosvaloresqueestamnapilhadememória3+2+7

Recursivemethodillustration:

    
27.03.2018 / 16:36
-1

From what I understand of your question you want the following (and using recursion):

#include <stdio.h>

int somarDigitos(int valor)
{
    if (valor == 0)
        return 0;
    else 
        return (valor % 10) + somarDigitos(valor /= 10);
}


int main(void) {

    int soma = somarDigitos(372);
    printf("%d\n", soma);
    return 0;
}

What the function does is always get the last element of the number through the mod (%) function, which takes the result of the division by 10, and calls the function again by dividing the value by 10 , thus eliminating the last digit until it reaches the last digit.

    
27.03.2018 / 16:02