{"id":20266,"date":"2021-09-09T19:57:37","date_gmt":"2021-09-09T14:27:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=20266"},"modified":"2021-11-22T18:36:24","modified_gmt":"2021-11-22T13:06:24","slug":"python-program-to-multiply-two-numbers-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-multiply-two-numbers-using-recursion\/","title":{"rendered":"Python Program to Multiply Two Numbers Using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Subtraction of Two Numbers using Recursion<\/a><\/p>\n Given two numbers and the task is to find the multiplication of the given two numbers using recursion.<\/p>\n Recursion:<\/strong><\/p>\n Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder \/Preorder\/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.<\/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 Below are the ways to find the multiplication of the given two numbers using recursion :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you are learning Python then the Python Programming Example<\/a> is for you and gives you a thorough description of concepts for beginners, experienced programmers.<\/p>\n In the previous article, we have discussed Python Program to Find Subtraction of Two Numbers using Recursion Given two numbers and the task is to find the multiplication of the given two numbers using recursion. Recursion: Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known …<\/p>\nGiven First Number = 3\r\nGiven Second Number = 5<\/pre>\n
The Multiplication of { 3 * 5 } using recursion = 15<\/pre>\n
Given First Number = 6\r\nGiven Second Number = 9<\/pre>\n
The Multiplication of { 6 * 9 } using recursion = 54<\/pre>\n
Program to Multiply Two Numbers Using Recursion\u00a0in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say recur_mult which takes the two numbers as arguments\r\n# and returns the multiplication of the given two numbers using recursion.\r\n\r\n\r\ndef recur_mult(fst_numb, secnd_numb):\r\n # Check if the first number is less than the second number using the if conditional\r\n # statement.\r\n if fst_numb < secnd_numb:\r\n # If the statement is true, then return the second number, first number as arguments\r\n # to the recur_mult.{Recursive logic}.\r\n return recur_mult(secnd_numb, fst_numb)\r\n # Check if the second number is not equal to 0 using the elif conditional statement.\r\n elif secnd_numb != 0:\r\n # If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb - 1)\r\n # {Recursive Logic}.\r\n\r\n return fst_numb + recur_mult(fst_numb, secnd_numb - 1)\r\n else:\r\n # Else return 0.\r\n return 0\r\n\r\n\r\n# Give the first number as static input and store it in a variable.\r\nfst_numb = 5\r\n# Give the second number as static input and store it in another variable.\r\nsecnd_numb = 3\r\n# Pass the given two numbers as the arguments to recur_mult function.\r\n# Print the multiplication of given two numbers using recursion.\r\nprint(\"The Multiplication of {\", fst_numb, \"*\", secnd_numb,\r\n \"} using recursion =\", recur_mult(fst_numb, secnd_numb))\r\n<\/pre>\n
The Multiplication of { 5 * 3 } using recursion = 15<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say recur_mult which takes the two numbers as arguments\r\n# and returns the multiplication of the given two numbers using recursion.\r\n\r\n\r\ndef recur_mult(fst_numb, secnd_numb):\r\n # Check if the first number is less than the second number using the if conditional\r\n # statement.\r\n if fst_numb < secnd_numb:\r\n # If the statement is true, then return the second number, first number as arguments\r\n # to the recur_mult.{Recursive logic}.\r\n return recur_mult(secnd_numb, fst_numb)\r\n # Check if the second number is not equal to 0 using the elif conditional statement.\r\n elif secnd_numb != 0:\r\n # If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb - 1)\r\n # {Recursive Logic}.\r\n\r\n return fst_numb + recur_mult(fst_numb, secnd_numb - 1)\r\n else:\r\n # Else return 0.\r\n return 0\r\n\r\n\r\n# Give the first number as user input using the int(input()) function and \r\n# store it in a variable.\r\nfst_numb = int(input(\"Enter some random number = \"))\r\n#Give the second number as user input using the int(input()) function and \r\n# store it in another variable.\r\nsecnd_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given two numbers as the arguments to recur_mult function.\r\n# Print the multiplication of given two numbers using recursion.\r\nprint(\"The Multiplication of {\", fst_numb, \"*\", secnd_numb,\r\n \"} using recursion =\", recur_mult(fst_numb, secnd_numb))\r\n<\/pre>\n
Enter some random number = 7\r\nEnter some random number = 0\r\nThe Multiplication of { 7 * 0 } using recursion = 0<\/pre>\n
\n