Python Program to Perform Multiplication with Examples

Given two numbers and the task is to find the Multiplication of two numbers.

Also, given a list and the task is to find the Multiplication of all the elements in a given list.

Here we also use the Numpy module to perform the  Multiplication

Python numpy.prod() Function:

prod() in numpy accepts a list as an argument and returns the product of all the elements in the list. This function is quite useful and saves a significant amount of code. To use numpy.prod(), simply import numpy().

Examples:

Example1:

Input:

Given first number = 2
Given second number = 3

Output:

The Multiplication of given two numbers { 2 * 3 } =  6

Example2:

Input:

Given List = [4, 7, 3, 1, 5]

Output:

The Multiplication of all the elements of a given list [4, 7, 3, 1, 5] = 
420

Program to Perform Multiplication in Python with Examples

 

Method #1: Using (*) Operator (Static Input)

1) Multiplication using Functions

Approach:

  • Create a function say Multof_2numbers() which accepts the given two numbers as the arguments and returns the Multiplication of the given two numbers.
  • Inside the function, multiply the given two numbers and store it in a variable.
  • Return the above result.
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to the Multof_2numbers() function and store it in another variable.
  • Print the Multiplication of given two numbers
  • The Exit of the program.

Below is the implementation:

# Create a function say Multof_2numbers() which accepts the given two numbers
# as the arguments and returns the Multiplication of given two numbers.


def Multof_2numbers(gvn_num1, gvn_num2):
    # Inside the function, multiply the given two numbers and store it in a variable.
    rsltmult = gvn_num1 * gvn_num2
    # Return the above result.
    return(rsltmult)


# Give the first number as static input and store it in a variable.
gvn_num1 = 2
# Give the second number as static input and store it in another variable.
gvn_num2 = 3
# Pass the given two numbers as the arguments to the Multof_2numbers() function
# and store it in another variable.
fnl_mult = Multof_2numbers(gvn_num1, gvn_num2)
# Print the Multiplication of given two numbers
print(
    "The Multiplication of given two numbers {", gvn_num1, "*", gvn_num2, "} = ", fnl_mult)

Output:

The Multiplication of given two numbers { 2 * 3 } =  6

2)Multiplication of list Elements using for loop:

Approach:

  • Give the list as static input and store it in a variable.
  • Take a variable and initialize its value to 1.
  • Loop in the given list using the for loop.
  • Multiply the element of the given list to the above declared rslt_mul variable and store it in the same variable.
  • Print the Multiplication of all the elements of a given list.
  •  The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [4, 7, 3, 1, 5]
# Take a variable and initialize its value to 1.
rslt_mul = 1
# Loop in the given list using the for loop.
for elemnt in gvn_lst:
    # Multiply the element of the given list to the above declared rslt_mul variable
    # and store it in the same variable.
    rslt_mul = rslt_mul * elemnt
# Print the Multiplication of all the elements of a given list.
print("The Multiplication of all the elements of a given list", gvn_lst, "= ")
print(rslt_mul)

Output:

The Multiplication of all the elements of a given list [4, 7, 3, 1, 5] = 
420

3)Using numpy.prod() Function

Approach:

  • Give the list as static input and store it in a variable.
  • Import numpy module using the import keyword.
  • Pass the given list as an argument to the numpy.prod() function to get the multiplication of all the elements of a given list.
  • Store it in another variable.
  • Print the Multiplication of all the elements of a given list.
  • The Exit of the program.

Below is the implementation:

# Import numpy module using the import keyword.
import numpy
# Give the list as static input and store it in a variable.
gvn_lst = [10, 2, 4]
# Pass the given list as an argument to the numpy.prod() function to get
# the multiplication of all the elements of a given list.
# Store it in another variable.
rslt_mul = numpy.prod(gvn_lst)
# Print the Multiplication of all the elements of a given list.
print("The Multiplication of all the elements of a given list", gvn_lst, "= ")
print(rslt_mul)

Output:

The Multiplication of all the elements of a given list [10, 2, 4] = 
80

Method #2: Using (*) Operator (User Input)

1) Multiplication using Functions

Approach:

  • Create a function say Multof_2numbers() which accepts the given two numbers as the arguments and returns the Multiplication of the given two numbers.
  • Inside the function, multiply the given two numbers and store it in a variable.
  • Return the above result.
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to the Multof_2numbers() function and store it in another variable.
  • Print the Multiplication of given two numbers
  • The Exit of the program.

Below is the implementation:

# Create a function say Multof_2numbers() which accepts the given two numbers
# as the arguments and returns the Multiplication of given two numbers.


def Multof_2numbers(gvn_num1, gvn_num2):
    # Inside the function, multiply the given two numbers and store it in a variable.
    rsltmult = gvn_num1 * gvn_num2
    # Return the above result.
    return(rsltmult)


# Give the first number as user input using the int(input()) fucntion and store it in a variable.
gvn_num1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) fucntion and store it in another variable.
gvn_num2 = int(input("Enter some random number = "))
# Pass the given two numbers as the arguments to the Multof_2numbers() function
# and store it in another variable.
fnl_mult = Multof_2numbers(gvn_num1, gvn_num2)
# Print the Multiplication of given two numbers
print(
    "The Multiplication of given two numbers {", gvn_num1, "*", gvn_num2, "} = ", fnl_mult)

Output:

Enter some random number = 6
Enter some random number = 4
The Multiplication of given two numbers { 6 * 4 } = 24

2)Multiplication of list Elements using for loop:

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Take a variable and initialize its value to 1.
  • Loop in the given list using the for loop.
  • Multiply the element of the given list to the above declared rslt_mul variable and store it in the same variable.
  • Print the Multiplication of all the elements of a given list.
  •  The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
   
# Take a variable and initialize its value to 1.
rslt_mul = 1
# Loop in the given list using the for loop.
for elemnt in gvn_lst:
    # Multiply the element of the given list to the above declared rslt_mul variable
    # and store it in the same variable.
    rslt_mul = rslt_mul * elemnt
# Print the Multiplication of all the elements of a given list.
print("The Multiplication of all the elements of a given list", gvn_lst, "= ")
print(rslt_mul)

Output:

Enter some random List Elements separated by spaces = 2 3 6 4
The Multiplication of all the elements of a given list [2, 3, 6, 4] = 
144

3)Using numpy.prod() Function

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Import numpy module using the import keyword.
  • Pass the given list as an argument to the numpy.prod() function to get the multiplication of all the elements of a given list.
  • Store it in another variable.
  • Print the Multiplication of all the elements of a given list.
  • The Exit of the program.

Below is the implementation:

# Import numpy module using the import keyword.
import numpy
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the numpy.prod() function to get
# the multiplication of all the elements of a given list.
# Store it in another variable.
rslt_mul = numpy.prod(gvn_lst)
# Print the Multiplication of all the elements of a given list.
print("The Multiplication of all the elements of a given list", gvn_lst, "= ")
print(rslt_mul)

Output:

Enter some random List Elements separated by spaces = 5 2 3 1
The Multiplication of all the elements of a given list [5, 2, 3, 1] = 
30