Python Program to Multiply Two Numbers using Lambda Function

We will develop a Python program to multiply two numbers using lambda function. A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

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

Mathematically,

Inputs: a=3, b=7
Product = a x b = 3 x 7 = 21

Multiply Two Numbers in Python using Lambda Function

We will take two numbers while declaring the variables and calculate the product of these numbers. Its multiplication value will be stored in the product variable and finally, the multiplication value will be displayed on the screen.

Program description: Write a python program to multiply two numbers using the lambda function

# Python program to multiply two numbers using lambda function

# lambda function
product = lambda a, b : a * b

# take inputs
a = 5
b = 7

# calculate product of numbers
product = a * b

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

Output:-

The product of number: 35

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to multiply two numbers using lambda function

# lambda function
product = lambda a, b : a * b

# take inputs
a = float(input('Enter first number: '))
b = float(input('Enter second number: '))

# calculate product of numbers
product = a * b

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

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

Enter first number: 9
Enter second number: 3
The product of number: 27.0

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

Enter first number: 25
Enter second number: 5.3
The product of number: 132.5

Python Program to Multiply Two Numbers using Lambda Function

We can also take the help of a function to multiply two numbers in python. A function is a block of code that performs a specific task.

# Python program to multiply two numbers using lambda function

# lambda function
product = lambda a, b : a * b

def product_num(a,b):   #user-defined function
   return a * b

# 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:", product)

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

Enter first number: 56
Enter second number: 36
The product of number: 2016.0

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

Enter first number: 5.6
Enter second number: 3.2
The product of number: 17.92

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

Enter first number: 21.98
Enter second number: 0.65
The product of number: 14.287

Leave a Comment

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