How to find the product of 2 numbers using recursion in C#?

Finding the product of two numbers using recursion in C# is an interesting approach that demonstrates how multiplication can be implemented using only addition and recursive function calls. Instead of using the standard multiplication operator, we build the product by repeatedly adding one number to itself.

How Recursive Multiplication Works

The concept is based on the mathematical principle that multiplication is repeated addition. For example, 5 × 3 = 5 + 5 + 5. In recursion, we add the first number to itself and reduce the second number by 1 until it reaches zero.

Recursive Multiplication: 6 × 3 product(6, 3) 6 + product(6, 2) 6 + 6 + product(6, 1) 6 + 6 + 6 + 0 Result: 18

Syntax

The recursive multiplication method follows this pattern −

public int product(int a, int b) {
   if (b == 0) return 0;           // Base case
   return a + product(a, b - 1);   // Recursive case
}

Example

Basic Recursive Multiplication

using System;

class Calculation {
   public static void Main() {
      int val1, val2, res;
      
      // the two numbers
      val1 = 10;
      val2 = 20;
      
      // finding product
      Demo d = new Demo();
      res = d.product(val1, val2);
      Console.WriteLine("{0} x {1} = {2}", val1, val2, res);
   }
}

class Demo {
   public int product(int val1, int val2) {
      if (val1 < val2) {
         return product(val2, val1);
      } else if (val2 != 0) {
         return (val1 + product(val1, val2 - 1));
      } else {
         return 0;
      }
   }
}

The output of the above code is −

10 x 20 = 200

Simplified Recursive Multiplication

using System;

class RecursiveMultiplication {
   public static int Multiply(int a, int b) {
      // Base case: if b is 0, product is 0
      if (b == 0) return 0;
      
      // Handle negative numbers
      if (b < 0) return -Multiply(a, -b);
      
      // Recursive case: a + multiply(a, b-1)
      return a + Multiply(a, b - 1);
   }
   
   public static void Main() {
      int num1 = 7;
      int num2 = 4;
      
      int result = Multiply(num1, num2);
      Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
      
      // Test with negative number
      int result2 = Multiply(5, -3);
      Console.WriteLine("5 x (-3) = {0}", result2);
   }
}

The output of the above code is −

7 x 4 = 28
5 x (-3) = -15

Optimized Version with Efficiency Check

using System;

class OptimizedRecursion {
   public static int MultiplyOptimized(int a, int b) {
      // Base case
      if (b == 0) return 0;
      if (b == 1) return a;
      
      // Optimization: always make the smaller number the multiplier
      if (Math.Abs(a) < Math.Abs(b)) {
         return MultiplyOptimized(b, a);
      }
      
      // Handle negative multiplier
      if (b < 0) return -MultiplyOptimized(a, -b);
      
      // Recursive multiplication
      return a + MultiplyOptimized(a, b - 1);
   }
   
   public static void Main() {
      Console.WriteLine("3 x 12 = " + MultiplyOptimized(3, 12));
      Console.WriteLine("15 x 2 = " + MultiplyOptimized(15, 2));
      Console.WriteLine("6 x (-4) = " + MultiplyOptimized(6, -4));
   }
}

The output of the above code is −

3 x 12 = 36
15 x 2 = 30
6 x (-4) = -24

Key Points

  • Base Case: When the second number becomes 0, return 0 to stop recursion.

  • Recursive Case: Add the first number to the result of multiplying the first number by (second number - 1).

  • Optimization: Always use the smaller absolute value as the multiplier to reduce recursive calls.

  • Negative Numbers: Handle negative multipliers by negating the final result.

Conclusion

Recursive multiplication demonstrates how complex operations can be broken down into simpler repeated operations. While not practical for large numbers due to stack overflow risks, it's an excellent example of recursive problem-solving and helps understand the fundamental relationship between multiplication and addition.

Updated on: 2026-03-17T07:04:35+05:30

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements