• 0
Sign in to follow this  
frw

Recusividade

Question

Galera to com um problema, a questão é a seguinte, preciso multiplicar n1 atraves de somas sucessivas, n2 irá determinar o total. mas o retorno é sempre o 1º elemento da pilha. eu tenho que retornar o ultimo! plx, help me!

Tai o codigo do que eu fiz.

int soma (int, int, int);
main()
{
    int num1,num2,resul;
    scanf("%d",&num1);
    scanf("%d",&num2);
    resul=soma(num1,num2,0);
    printf("%d",resul);
    getch();
}
int soma(n1,n2,r)
{
    if(n2!=0)
    {
        r=r+n1;
        soma(n1,n2-1,r);
    }
}           

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Eu fiz assim e deu certo:

int soma(int n1, int n2, int r)
{
   if(n2!=0)
   {
       r = r + n1 + soma(n1,n2-1,r);
   }
   return r;
}           
int main()
{
   int num1,num2,resul;

   printf("digite o valor num1: ");
   scanf("%d",&num1);

   printf("digite o valor num2: ");
   scanf("%d",&num2);

   resul = soma( num1, num2, 0 );

   printf("Resultado final: %d \n",resul);

   getch();
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this