{"id":25880,"date":"2021-12-16T09:20:46","date_gmt":"2021-12-16T03:50:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=25880"},"modified":"2021-12-16T09:20:46","modified_gmt":"2021-12-16T03:50:46","slug":"python-program-to-perform-multiplication-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-perform-multiplication-with-examples\/","title":{"rendered":"Python Program to Perform Multiplication with Examples"},"content":{"rendered":"
Given two numbers and the task is to find the Multiplication of two numbers.<\/p>\n
Also, given a list and the task is to find the Multiplication of all the elements in a given list.<\/p>\n
Here we also use the Numpy module to perform the\u00a0 Multiplication<\/p>\n
Python numpy.prod() Function:<\/strong><\/p>\n 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<\/strong>().<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n 1) Multiplication using Functions<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Multiplication of list Elements using for loop:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3)Using numpy.prod() Function<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 1) Multiplication using Functions<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Multiplication of list Elements using for loop:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3)Using numpy.prod() Function<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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\u00a0 Multiplication Python numpy.prod() Function: prod() in numpy accepts a list as …<\/p>\nGiven first number = 2\r\nGiven second number = 3<\/pre>\n
The Multiplication of given two numbers { 2 * 3 } = 6<\/pre>\n
Given List = [4, 7, 3, 1, 5]<\/pre>\n
The Multiplication of all the elements of a given list [4, 7, 3, 1, 5] = \r\n420<\/pre>\n
Program to Perform Multiplication in Python with Examples<\/h2>\n
Method #1: Using (*) Operator (Static Input)<\/h3>\n
\n
# Create a function say Multof_2numbers() which accepts the given two numbers\r\n# as the arguments and returns the Multiplication of given two numbers.\r\n\r\n\r\ndef Multof_2numbers(gvn_num1, gvn_num2):\r\n # Inside the function, multiply the given two numbers and store it in a variable.\r\n rsltmult = gvn_num1 * gvn_num2\r\n # Return the above result.\r\n return(rsltmult)\r\n\r\n\r\n# Give the first number as static input and store it in a variable.\r\ngvn_num1 = 2\r\n# Give the second number as static input and store it in another variable.\r\ngvn_num2 = 3\r\n# Pass the given two numbers as the arguments to the Multof_2numbers() function\r\n# and store it in another variable.\r\nfnl_mult = Multof_2numbers(gvn_num1, gvn_num2)\r\n# Print the Multiplication of given two numbers\r\nprint(\r\n \"The Multiplication of given two numbers {\", gvn_num1, \"*\", gvn_num2, \"} = \", fnl_mult)\r\n<\/pre>\n
The Multiplication of given two numbers { 2 * 3 } = 6<\/pre>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [4, 7, 3, 1, 5]\r\n# Take a variable and initialize its value to 1.\r\nrslt_mul = 1\r\n# Loop in the given list using the for loop.\r\nfor elemnt in gvn_lst:\r\n # Multiply the element of the given list to the above declared rslt_mul variable\r\n # and store it in the same variable.\r\n rslt_mul = rslt_mul * elemnt\r\n# Print the Multiplication of all the elements of a given list.\r\nprint(\"The Multiplication of all the elements of a given list\", gvn_lst, \"= \")\r\nprint(rslt_mul)\r\n<\/pre>\n
The Multiplication of all the elements of a given list [4, 7, 3, 1, 5] = \r\n420<\/pre>\n
\n
# Import numpy module using the import keyword.\r\nimport numpy\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [10, 2, 4]\r\n# Pass the given list as an argument to the numpy.prod() function to get\r\n# the multiplication of all the elements of a given list.\r\n# Store it in another variable.\r\nrslt_mul = numpy.prod(gvn_lst)\r\n# Print the Multiplication of all the elements of a given list.\r\nprint(\"The Multiplication of all the elements of a given list\", gvn_lst, \"= \")\r\nprint(rslt_mul)\r\n<\/pre>\n
The Multiplication of all the elements of a given list [10, 2, 4] = \r\n80<\/pre>\n
Method #2: Using (*) Operator (User Input)<\/h3>\n
\n
# Create a function say Multof_2numbers() which accepts the given two numbers\r\n# as the arguments and returns the Multiplication of given two numbers.\r\n\r\n\r\ndef Multof_2numbers(gvn_num1, gvn_num2):\r\n # Inside the function, multiply the given two numbers and store it in a variable.\r\n rsltmult = gvn_num1 * gvn_num2\r\n # Return the above result.\r\n return(rsltmult)\r\n\r\n\r\n# Give the first number as user input using the int(input()) fucntion and store it in a variable.\r\ngvn_num1 = int(input(\"Enter some random number = \"))\r\n# Give the second number as user input using the int(input()) fucntion and store it in another variable.\r\ngvn_num2 = int(input(\"Enter some random number = \"))\r\n# Pass the given two numbers as the arguments to the Multof_2numbers() function\r\n# and store it in another variable.\r\nfnl_mult = Multof_2numbers(gvn_num1, gvn_num2)\r\n# Print the Multiplication of given two numbers\r\nprint(\r\n \"The Multiplication of given two numbers {\", gvn_num1, \"*\", gvn_num2, \"} = \", fnl_mult)\r\n<\/pre>\n
Enter some random number = 6\r\nEnter some random number = 4\r\nThe Multiplication of given two numbers { 6 * 4 } = 24<\/pre>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n \r\n# Take a variable and initialize its value to 1.\r\nrslt_mul = 1\r\n# Loop in the given list using the for loop.\r\nfor elemnt in gvn_lst:\r\n # Multiply the element of the given list to the above declared rslt_mul variable\r\n # and store it in the same variable.\r\n rslt_mul = rslt_mul * elemnt\r\n# Print the Multiplication of all the elements of a given list.\r\nprint(\"The Multiplication of all the elements of a given list\", gvn_lst, \"= \")\r\nprint(rslt_mul)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 2 3 6 4\r\nThe Multiplication of all the elements of a given list [2, 3, 6, 4] = \r\n144<\/pre>\n
\n
# Import numpy module using the import keyword.\r\nimport numpy\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the numpy.prod() function to get\r\n# the multiplication of all the elements of a given list.\r\n# Store it in another variable.\r\nrslt_mul = numpy.prod(gvn_lst)\r\n# Print the Multiplication of all the elements of a given list.\r\nprint(\"The Multiplication of all the elements of a given list\", gvn_lst, \"= \")\r\nprint(rslt_mul)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 5 2 3 1\r\nThe Multiplication of all the elements of a given list [5, 2, 3, 1] = \r\n30<\/pre>\n","protected":false},"excerpt":{"rendered":"