Q. Write a program to print product of 2 numbers using recursion.

Given two number x and y as input our task is to print product of 2 numbers using recursion.

For Example:-
Input : x = 5, y = 2
Output : 10
Logic : If x is less than y, swap the two variables value and then recursively find y times the sum of x till if any of them become zero and return 0.

Algorithm

START
Step 1 : Input number x and y
Step 2 : If x is less than y : 
            swap the number
         Else
            recursively find y times the sum of x 
        End If
STOP


Program for Product of 2 Numbers using Recursion

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    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;
    }
     
    int main()
    {
        int x = 10, y = 2;
        printf("%d",product(x, y));
        return 0;
    }
    
    #include <iostream>
    using namespace std;
    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;
    }
     
    int main()
    {
        int x =10, y = 2;
        cout << product(x, y);
        return 0;
    }
    
    class LFC
    {
         
        // 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;
        }
    
        public static void main (String[] args)
        {
            int x = 10, y = 2;
            System.out.println(product(x, y));
        }
    }
    
    # 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
     
    x = 10
    y = 2
    print( product(x, y))
    
    using System;
    class LFC
    {
         
        // 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;
        }
    
        public static void Main ()
        {
            int x = 10, y = 2;
            Console.WriteLine(product(x, y));
        }
    }
    
    <?php
    
    // 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;
    }
     
    $x = 10; $y = 2;
    echo(product($x, $y));
    
    #include <stdio.h>
    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;
    }
     
    int main()
    {
        int x = 10, y = 2;
        printf("%d",product(x, y));
        return 0;
    }
    

    Output

    20
    

    Recommended Programs

     Insertion sort program
     Program to check panagram