Product of Two Numbers Last Updated : 17 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given two numbers, a and b. Return the product of both the numbers.Examples:Input: a = 4, b = 5Output: 20Input: a = 3, b = 5Output: 15Table of Content[Expected Approach] Using Multiplication Operator - O(1) Time and O(1) Space[Alternate Approach] Using Recursion - O(min(a, b)) Time and O(min(a, b)) Space[Expected Approach] Using Multiplication Operator - O(1) Time and O(1) SpaceWe can find the product of both the numbers by using the multiplication operator (*). This operator works by taking two operands and returns their product as the result. C++ #include <iostream> using namespace std; int main() { int a = 4, b = 5; // print the product of a and b cout << (a * b) << endl; return 0; } Java class GfG { public static void main(String[] args) { int a = 4, b = 5; // print the product of a and b System.out.println(a * b); } } Python if __name__ == "__main__": a = 4 b = 5 # print the product of a and b print(a * b) JavaScript // Driver Code let a = 4, b = 5; // print the product of a and b console.log(a * b); Output20 [Alternate Approach] Using Recursion - O(min(a, b)) Time and O(min(a, b)) SpaceTo find the product of two numbers, x and y, using recursion, you can follow this approach:Base Case: If y equals 0, return 0 (since any number multiplied by 0 results in 0).Recursive Case: Add x to the result and make a recursive call with y decremented by 1.To know more about the implementation, please refer to Product of 2 Numbers using Recursion. Comment More infoAdvertise with us Next Article Product of Two Numbers G garvitaquwv Follow Improve Article Tags : Mathematical DSA Practice Tags : Mathematical Similar Reads Product of 2 Numbers using Recursion Given two numbers x and y find the product using recursion.Examples : Input : x = 5, y = 2Output : 10Input : x = 100, y = 5Output : 500To find the product of two numbers x and y using recursion, you can use the following approach:Base Case: If y=0, return 0 (since any number multiplied by 0 is 0).Re 4 min read Product of factors of number Given a number n, find the product of all factors of n. Since the product can be very large answer it modulo 10^9 + 7.Examples : Input : 12 Output : 1728 1 * 2 * 3 * 4 * 6 * 12 = 1728 Input : 18 Output : 5832 1 * 2 * 3 * 6 * 9 * 18 = 5832 Recommended PracticeProduct of factors of numberTry It! Metho 11 min read Number of digits in the product of two numbers Given two integers a and b. The problem is to find the number of digits in the product of these two integers.Examples: Input : a = 12, b = 4 Output : 2 12 * 4 = 48 (2 digits) Input : a = 33, b = -24 Output : 3 33 * -24 = -792 (3 digits)Recommended PracticeProduct SumTry It! Basic Approach: Multiply 8 min read 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 recu 8 min read Maximum Sum of Products of Two Arrays Given two arrays A and B of positive integers of the same size N. The task is to find the maximum sum of products of their elements. Each element in A has to be multiplied with exactly one element in B or vice versa such that each element of both the arrays appears exactly once and the sum of the pr 5 min read Like