Python Program to Multiply Two Numbers using Function

We will develop a Python program to multiply two numbers using function. We can take the help of a function to multiply two numbers. A function is a block of code that performs a specific task. Multiplication of two numbers in python using multiplication operator (*). Also, we will calculate the product of two numbers using recursion.

How to find the product of two number:
Product = a x b

Mathematically,

Inputs: a=2, b=5
Product = a x b = 2 x 5 = 10

Python Program for Multiplication of Two Numbers using Function

This is the simplest and easiest way to multiply two numbers in Python. We will take two numbers while declaring the variables and calculate the product of these numbers using a user-defined function. Its multiplication value will be stored in the product variable using the function call and finally, the multiplication value will be displayed on the screen.

# Python program to multiply two number using function

def product_num(num1, num2):  #user-defind function
    num = (num1 * num2)   #calculate product
    return num   #return value

# take inputs
num1 = 2
num2 = 3

# function call
product = product_num(num1, num2)

# print multiplication value
print("The Product of Number:", product)

Output:-

The Product of Number: 6

Python Program to Multiply Two Numbers using Function

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. Inputs are scanned using the input() function and stored in variable num1, and num2.

# Python program to multiply two number using function

def product_num(num1, num2):  #user-defind function
    num = (num1 * num2)   #calculate product
    return num   #return value

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# function call
product = product_num(num1, num2)

# print multiplication value
print("The Product of Number:", product)

Output for the input values test-case-1:-

Enter first number: 7
Enter second number: 3
The Product of Number: 21

Output for the input values test-case-2:-

Enter first number: 23
Enter second number: 46
The Product of Number: 1058

In the previous program, we calculated the product of two integer numbers but in this program, calculate the product of two float numbers.

# Python program to multiply two number using function

def product_num(num1, num2):  #user-defind function
    num = (num1 * num2)   #calculate product
    return num   #return value

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# function call
product = product_num(num1, num2)

# print multiplication value
print("The Product of Number: %0.2f" %product)

Output for the input values test-case-1:-

Enter first number: 25
Enter second number: 4.3
The Product of Number: 107.50

Output for the input values test-case-2:-

Enter first number: 23.9
Enter second number: 12.3
The Product of Number: 293.97

Product of Two Numbers in Python using Recursion

A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can be handled easily. This is also a well-known computer programming technique: divide and conquer.

# Python program to multiply two number using recursion

def product_num(num1,num2):   #user-defined function
    if(num1<num2):
        return product_num(num2,num1)
    elif(num2!=0):
         return(num1+product_num(num1,num2-1))
    else:
         return 0

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# function call
product = product_num(num1, num2)

# print multiplication value
print("The Product of Number:", product)

Output for the input values test-case-1:-

Enter first number: 25
Enter second number: 8
The Product of Number: 200

Also See:- Python Program to Find Average of Two Numbers

Leave a Comment

Your email address will not be published. Required fields are marked *