Recursive multiplication in C

2

I'm learning recursion in C, and I was able to do some programming using recursion. However, I'm picking up to make a simple program that the user sending two numbers, example: 6x2, I multiply them in recursion. I may even show you the code below, but I'm sure it escapes entirely from the reality of the actual code.

Code:

#include <stdio.h>

int poligono(int n,int v){
    int res=0;

    if(v==0)
        return 0;
    else if(n==0)
        return n;
    else
        return n + poligono(n,v);
}

int main(){

    int n,v,res=0;
    scanf("%d%d",&n,&v);
    res=poligono(n,v);
    printf("Res = %d\n",res);

    return 0;

}
    
asked by anonymous 29.08.2016 / 14:26

4 answers

2

Some points to improve:

  • Checking on the first if is unnecessary, you need to treat n which is the number of times your code is iterated, never v , which must be a fixed value to be summed with itself by n times.
  • Never return n , only v .
  • The value of n must be decremented at each iteration, or incremented, if n is a negative value.
  • Negative values are not treated in your algorithm.
  • Putting it all together:

    #include <stdio.h>
    
    int poligono(int n, int v){
        int vs;
        if(n>0){
            n--;
            vs = v;
        }
        else if (n<0){
            n++;
            vs = -v;
        }
    
        if(n==0)
            return vs;
        else
            return vs + poligono(n,v);
    }
    
    int main(){
    
        int n,v,res=0;
        scanf("%d%d",&n,&v);
        res=poligono(n,v);
        printf("Res = %d\n",res);
    
        return 0;
    
    }
    

    See an example on Ideone: link

        
    29.08.2016 / 15:24
    2

    First, an issue that has to be obeyed in recursion is that it is finite, that is, there must be an exit from your program. If you want to do a recursive multiplication, it would look like this:

    int poligono( int a, int b ){
    
       if( b == 1)
         return a;
       return a + poligono(a, --b);
    }
    
        
    29.08.2016 / 20:55
    1

    A recursive algorithm always assumes the use of the same algorithm in a simpler problem, until eventually the problem is so simple that it is no longer necessary to use recursion.

    In your example, the     return n + poligono(n,v); should be     return n + poligono(n-1,v);

    Note that I'm not even analyzing if your program works correctly, I'm just solving the problem of infinite recursion.

    (I am also assuming that eventually you will put some consistency to avoid using negative values.)

        
    29.08.2016 / 14:59
    0

    The recursive method for multiplying two factors is 3 + 3 + 3 + 3 = 12: first think about how multiplication is done. 3 * 4 = 12 is the same as 3 + 4 * 2 which means the same as 3 + (4 * (3-1)) recursively

    int poligono(int a , int b) {
            if ((a == 0)||(b==0))
            {
                return 0;
            }
            else {
                return a + poligono(b,(a-1));
            }
        }
    
        
    04.03.2018 / 16:55