×

Recursive Multiplication Python

Recursive Multiplication Python

In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach.

In the recursive multiplication approach, we will use the following steps for writing the program:

Step 1: We will take two integers as user input and store them in two different variables, i.e., num1 & num2.

Step 2: If num1 is less than num2, we will swap two variables inside the function. We will also compare the values inside the function.

Step 3: We will find the num2 times sum of the num1 variable using the recursion method.

Step 4: If any of the variables become zero, the function will return 0 in output.

Step 5: Print the result of multiplication in the output of the program.

Let's understand the implementation of above steps through the following Python program:

Example – 

 # Define a default recursive multiplication function
 def RecurMulti (m, n):
     if m < n: # checking which number is greater
         return RecurMulti (n, m) # swapping numbers
     elif n != 0: # if number is not 0
         return (m + RecurMulti (m, n - 1)) # recursive multiplication
     else:
         return 0 # returning 0 in every other case
 # Taking both numbers as input from user
 num1 = int (input ("Enter the first positive number :"))
 num2 = int (input ("Enter the second positive number :"))
 # Storing the result of Multiplication of both number
 Product_Result = RecurMulti (num1, num2)
 # Printing result in output
 print ("The result of multiplication of two number is: ", Product_Result) 

Output:

 Enter the first positive number : 5
 Enter the second positive number : 32
 The result of the multiplication of two number is:  160 

We can use this recursive approach in our Python program to find the product of any two positive numbers given by the user.


Related Topics

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Python Array

Python Array In the programming language or computer science, an array is defined as the form of a data structure which consists of or store collection of various types of elements...

10 minutes read.

Curdir Python

Python Programming Language Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Import Module in Python

In this article, you will learn everything about “ Import ” in Python. Modules in python that are already created can be accessed and used in another code by importing the...

6 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

Python vs HTML

Python and HTML are not comparable since they are two separate categories of programming languages. Building the structures and layouts of a web page or app requires the usage of HTML,...

8 minutes read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.