Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Product of Two Numbers
Next article icon

Product of Two Numbers

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given two numbers, a and b. Return the product of both the numbers.

Examples:

Input: a = 4, b = 5
Output: 20

Input: a = 3, b = 5
Output: 15

Table 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) Space

We 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); 

Output
20 

[Alternate Approach] Using Recursion - O(min(a, b)) Time and O(min(a, b)) Space

To 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.


Next Article
Product of Two Numbers

G

garvitaquwv
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences