The Wayback Machine - https://web.archive.org/web/20240717180320/https://www.geeksforgeeks.org/product-2-numbers-using-recursion/
Open In App

Product of 2 Numbers using Recursion

Last Updated : 20 Feb, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Given two numbers x and y find the product using recursion.

Examples : 

Input : x = 5, y = 2
Output : 10

Input : x = 100, y = 5
Output : 500

Method 
1) If x is less than y, swap the two variables value 
2) Recursively find y times the sum of x 
3) If any of them become zero, return 0 

C++




// C++ Program to find Product 
// of 2 Numbers using Recursion
#include <bits/stdc++.h>
using namespace std;
  
// recursive function to calculate
// multiplication of two numbers
int product(int x, int y)
{
    // if x is less than 
    // y swap the numbers
    if (x < y)
        return product(y, x);
  
    // iteratively calculate 
    // y times sum of x
    else if (y != 0)
        return (x + product(x, y - 1));
  
    // if any of the two numbers is 
    // zero return zero
    else
        return 0;
}
  
// Driver Code
int main()
{
    int x = 5, y = 2;
    cout << product(x, y);
    return 0;
}


Java




// Java Program to find Product
// of 2 Numbers using Recursion
import java.io.*;
import java.util.*;
  
class GFG 
{
      
    // recursive function to calculate 
    // multiplication of two numbers
    static int product(int x, int y)
    {
        // if x is less than 
        // y swap the numbers
        if (x < y)
            return product(y, x);
      
        // iteratively calculate 
        // y times sum of x
        else if (y != 0)
            return (x + product(x, y - 1));
      
        // if any of the two numbers is 
        // zero return zero
        else
            return 0;
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        int x = 5, y = 2;
        System.out.println(product(x, y)); 
    }
}
  
// This code is contributed by Gitanjali.


Python3




# Python3 to find Product of
# 2 Numbers using Recursion
  
# recursive function to calculate
# multiplication of two numbers 
def product( x , y ):
    # if x is less than y swap
    # the numbers
    if x < y:
        return product(y, x)
      
    # iteratively calculate y
    # times sum of x
    elif y != 0:
        return (x + product(x, y - 1))
      
    # if any of the two numbers is
    # zero return zero
    else:
        return 0
  
# Driver code
x = 5
y = 2
print( product(x, y))
  
# This code is contributed
# by Abhishek Sharma44.


C#




// C# Program to find Product
// of 2 Numbers using Recursion
using System;
  
class GFG 
{
      
    // recursive function to calculate 
    // multiplication of two numbers
    static int product(int x, int y)
    {
        // if x is less than 
        // y swap the numbers
        if (x < y)
            return product(y, x);
      
        // iteratively calculate 
        // y times sum of x
        else if (y != 0)
            return (x + product(x, y - 1));
      
        // if any of the two numbers is 
        // zero return zero
        else
            return 0;
    }
      
    // Driver code
    public static void Main ()
    {
        int x = 5, y = 2;
        Console.WriteLine(product(x, y)); 
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP Program to find Product 
// of 2 Numbers using Recursion
  
// recursive function to calculate
// multiplication of two numbers
function product($x, $y)
{
    // if x is less than  
    // y swap thenumbers
    if ($x < $y)
        return product($y, $x);
  
    // iteratively calculate 
    // y times sum of x
    else if ($y != 0)
        return ($x + product($x, $y - 1));
  
    // if any of the two numbers is 
    // zero return zero
    else
        return 0;
}
  
// Driver Code
$x = 5; $y = 2;
echo(product($x, $y));
  
// This code is contributed by Ajit.
?>


Javascript




<script>
// JavaScript program to find Product 
// of 2 Numbers using Recursion
  
// recursive function to calculate
// multiplication of two numbers
function product(x, y)
{
    // if x is less than  
    // y swap thenumbers
    if (x < y)
        return product(y, x);
  
    // iteratively calculate 
    // y times sum of x
    else if (y != 0)
        return (x + product(x, y - 1));
  
    // if any of the two numbers is 
    // zero return zero
    else
        return 0;
}
  
// Driver Code
let x = 5;
let y = 2;
document.write(product(x, y));
  
// This code is contributed by mohan.
  
</script>


Output

10

Time Complexity: O(min(x,y))
Auxiliary Space: O(min(x,y)), The extra space is used in the recursive call stack.



Previous Article
Next Article

Similar Reads

Product of 2 numbers using recursion | Set 2
Given two numbers N and M. The task is to find the product of the 2 numbers using recursion.Note: The numbers can be both positive or negative. Examples: Input : N = 5 , M = 3 Output : 15 Input : N = 5 , M = -3 Output : -15 Input : N = -5 , M = 3 Output : -15 Input : N = -5 , M = -3 Output:15 A recursive solution to the above problem for only posit
8 min read
Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers
Given a number N (N &gt; 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers ≤ N that are even and can be expressed as the product of two distinct prime numbers. Example: Input: N = 6Output: 2→1 3→1Explanation: 6 is the only number ≤ N, which is even and a product of two distinct prime numbers (2 an
7 min read
Product of nodes at k-th level in a tree represented as string using Recursion
Prerequisite: Product of nodes at k-th level in a tree represented as stringGiven an integer ‘K’ and a binary tree in string format. Every node of a tree has value in range from 0 to 9. We need to find product of elements at K-th level from the root. The root is at level 0.Note: Tree is given in the form: (node value(left subtree)(right subtree))Ex
5 min read
Sum of natural numbers using recursion
Given a number n, find sum of first n natural numbers. To calculate the sum, we will use a recursive function recur_sum().Examples : Input : 3 Output : 6 Explanation : 1 + 2 + 3 = 6 Input : 5 Output : 15 Explanation : 1 + 2 + 3 + 4 + 5 = 15 Below is code to find the sum of natural numbers up to n using recursion : C/C++ Code // C++ program to find
3 min read
Find HCF of two numbers without using recursion or Euclidean algorithm
Given two integer x and y, the task is to find the HCF of the numbers without using recursion or Euclidean method. Examples: Input: x = 16, y = 32 Output: 16 Input: x = 12, y = 15 Output: 3 Approach: HCF of two numbers is the greatest number which can divide both the numbers. If the smaller of the two numbers can divide the larger number then the H
6 min read
Print even and odd numbers in a given range using recursion
Given two integers L and R, the task is to print all the even and odd numbers from L to R using recursion. Examples: Input: L = 1, R = 10Output: Even numbers: 2 4 6 8 10Odd numbers: 1 3 5 7 9 Input: L = 10, R = 25 Output: Even numbers:10 12 14 16 18 20 22 24 Odd numbers:11 13 15 17 19 21 23 25 Approach: Follow the steps below to solve the problem u
6 min read
C Program to find LCM of two numbers using Recursion
Given two integers N and M, the task is to find their LCM using recursion. Examples: Input: N = 2, M = 4Output: 4Explanation: LCM of 2, 4 is 4. Input: N = 3, M = 5Output: 15Explanation: LCM of 3, 5 is 15. Approach: The idea is to use the basic elementary method of finding LCM of two numbers. Follow the steps below to solve the problem: Define a rec
3 min read
Print numbers 1 to N using Indirect recursion
Given a number N, we need to print numbers from 1 to N with out direct recursion, loops, labels. Basically we need to insert in above code snippet so that it can be able to print numbers from 1 to N? C/C++ Code #include &lt;stdio.h&gt; #define N 20; int main() { // Your code goes Here. } Examples : Input : 10 Output : 1 2 3 4 5 6 7 8 9 10 Input : 5
5 min read
Decimal to Binary using recursion and without using power operator
Given an integer N, the task is convert and print the binary equaiva;ent of N.Examples: Input: N = 13 Output: 1101Input: N = 15 Output: 1111 Approach Write a recursive function that takes an argument N and recursively calls itself with the value N / 2 as the new argument and prints N % 2 after the call. The base condition will be when N = 0, simply
3 min read
Practice Tags :
three90RightbarBannerImg