How to multiply 2 numbers using recursion

product of 2 numbers using recursion in c
sum of 2 numbers using recursion in c
division of two numbers using recursion in c
multiply two numbers without using + operator in c++
count zeros using recursion
write a program to display first four multiples of a number using recursion
multiplication table using recursion in c
convert string to integer using recursion

I'm trying to multiply (3, 6) and (9, 9) using recursion. However, the result printed is 18 and 45. I need to find out which part is wrong.

Here's my code:

#include <stdio.h>

int multiply (int, int);

int main()
{
    int a, b, c;
    a = 6; b = 3; c = multiply(a, b);
    printf("%d\n", c);
    a = 9; b = 9; c = multiply(a, b);
    printf("%d\n", c);
    return 0;
}

int multiply(int a, int b)
{
    static int c = 0, i = 0;
    if (i < a) {
        c = c + b;
        i++;
        multiply(a, b);
    }

    return c;
}

The issue is that multiply's static variables persist from call to call, which throws the second calculation off. It is possible to bandage this wound, but it's better to address the underlying design problem that is compelling use of static variables in the first place. There is no need to artificially maintain state in the function using i (the number of additions to perform) and c (a product accumulator).

Given that multiplication is repeated addition of a b times, you can establish a base case of b == 0 and recursively add a, incrementing or decrementing b (depending on b's sign) until it reaches 0. The product accumulator c is replaced by the function return value and the number of multiplications i is represented by b.

Using this approach, each stack frame's state is naturally self-reliant.

#include <stdio.h>

int multiply(int a, int b) {
    if (b > 0) {
        return a + multiply(a, b - 1);
    }
    else if (b < 0) {
        return -a + multiply(a, b + 1);
    }

    return 0;
}

int main() {
    printf("%d\n", multiply(3, 6));
    printf("%d\n", multiply(9, 9));
    printf("%d\n", multiply(-6, 2));
    printf("%d\n", multiply(6, -2));
    printf("%d\n", multiply(-7, -3));
    printf("%d\n", multiply(0, 7));
    printf("%d\n", multiply(7, 0));
    printf("%d\n", multiply(0, 0));
    return 0;
}

Output:

18
81
-12
-12
21
0
0
0

As a final note, I recommend following proper code style. Minifying your code and using single-character variable names only makes debugging more difficult (someone has since de-minified the original code in an edit).

Product of 2 Numbers using Recursion, The issue is that multiply 's static variables persist from call to call, which throws the second calculation off. It is possible to bandage this wound,  C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++

Both c and i need to be reset to zero on each [outer] call to multiply [as others have mentioned] because a function scope static variable is only initialized once.

There is no way to do this because the static variables are at multiply function scope (i.e. how does main access/reset them?). They would need to be moved to global/file scope.

Adding a helper function and moving the variables to global scope will do it:

#include <stdio.h>

int multiply(int, int);

int
main()
{
    int a,
     b,
     c;

    a = 6;
    b = 3;
    c = multiply(a, b);
    printf("%d\n", c);

    a = 9;
    b = 9;
    c = multiply(a, b);
    printf("%d\n", c);

    return 0;
}

static int c, i;

int
mul(int a, int b)
{

    if (i < a) {
        c = c + b;
        i++;
        mul(a, b);
    }

    return c;
}

int
multiply(int a, int b)
{

    i = 0;
    c = 0;

    return mul(a,b);
}

How to multiply 2 numbers using recursion, This C program using recursion, finds the product of 2 numbers without using the multiplication operator. advertisement. Program/Source Code. Here is the  Assuming integers greater than zero. In the Python programming language: [code]>>> def mult(a, b): if b==1: return a else: return a+mult(a, b-1)

Try resetting your static variables before second call to multiply or do without them

int multiply(int a, int b) {
      If (a==0)
           return 1;
      else if (a>0)
           return b+multiply(a-1, b);
        else
          return - 1*multiply(-1*a, b); }

C Program to Find Product of 2 Numbers using Recursion, In this article, we are going to learn how to Find the product of two numbers using recursion in the C programming language. How to multiply 2 numbers using recursion. Ask Question Asked 1 year, 1 month ago. Active 1 month ago. Viewed 696 times 0. I'm trying to multiply (3, 6) and (9, 9) using recursion. However, the result printed is 18 and 45. I need to find out which part i

Recursion Function To Multiply Two Positive Integers, #Example 2: C program to multiply two numbers using recursion. //Multiplication of two numbers using recursive function #include <stdio.h> //function prototype for  Table of Content. Find Factorial of Number Using Recursion; C Program to print Tower of Hanoi using recursion !! Find Sum of Digits of the Number using Recursive Function in C Programming; C Program to calculate sum of numbers 1 to N using recursion; C Program to Multiply two Matrices using Recursion !! C Program to Print Fibonacci Series using

Find the product of two numbers in C using recursion, Firstly, set the two numbers to be multiplied. val1 = 10; val2 = 20; Now cal the method to find the product. product(val1, val2); Under the product  Write a C program to multiply two numbers without using * multiplication operator. Given two numbers, both numbers are positive. Write a program to multiply two numbers without using * multiplication operator. Think for a moment how to solve this problem . You can use multiple approaches to find the solution of a problem.

C program to multiply two numbers without using multiplication , Algorithm: To solve using recursion, define a recursion function with 2 parameters m and n (the numbers you want to multiply). By making use of recursion, we can multiply two integers with the given constraints. To multiply x and y, recursively add x y times. // C++ program to Multiply two integers without. // using multiplication, division and bitwise. // operators, and no loops. #include<iostream> using namespace std; /* function to multiply two numbers x and y*/

Comments
  • static variables are only initialized once. On subsequent calls to multiply, c and i will not be initialized again, but keep their previous values.
  • Either a or b should be reduced toward zero on the recursion call. There should be no need to static anything in this code.
  • Your code works for only positive integers. It gives 0 on multiplying 3 and -6
  • I realize that, but thanks--it's easily addressable with a helper or a sign test. I'll update.
  • Adding if (b < 0) return a + multiply(a, b + 1); would handle negative b values.
  • There is no way to do this because the static variables are at multiply function scope (i.e. how does main access/reset them?). They would need to be moved to global/file scope.
  • I can imagine a couple of ways to achieve it. Eg add third parameter to multiply. 1 means reset, 0 no reset. You might argue about whether it's aestethically pleasing or not.