{"id":9722,"date":"2021-09-30T17:30:54","date_gmt":"2021-09-30T12:00:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=9722"},"modified":"2021-11-22T18:33:31","modified_gmt":"2021-11-22T13:03:31","slug":"python-program-to-find-the-product-of-two-numbers-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-product-of-two-numbers-using-recursion\/","title":{"rendered":"Python Program to Find the Product of two Numbers Using Recursion"},"content":{"rendered":"
Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n Recursion in Python:<\/strong><\/p>\n When a function calls itself and loops until it reaches the intended end state, this is referred to as recursion. It is based on the mathematics idea of recursive definitions, which define elements in a set in terms of other members in the set.<\/p>\n Each recursive implementation contains a base case in which the desired state is reached, and a recursive case in which the desired state is not reached and the function enters another recursive phase.<\/p>\n On each step, the behavior in the recursive situation before the recursive function call, the internal self-call, is repeated. Recursive structures are beneficial when a larger problem (the base case) can be solved by solving repeated subproblems (the recursive case) that incrementally advance the program to the base case. In other words, recursion is declarative because you specify the desired state, whereas for\/while loops are iterative because you provide the number of repeats.<\/p>\n Given two numbers the task is to find the product of the given two numbers using recursion in Python.<\/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 Product of two Numbers using the recursive approach in Python:<\/strong><\/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 Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly. Recursion in Python: When a function calls itself and loops until it reaches the intended end state, this is referred to as recursion. It is based on the mathematics idea of recursive definitions, …<\/p>\n
\nIt behaves similarly to for and while loops, with the exception that recursion moves closer to the desired condition, whereas for loops run a defined number of times and while loops run until the condition is no longer met.<\/p>\ngiven the first number = 27 \r\ngiven the second number = 19<\/pre>\n
The product of the given numbers 27 and 19 is 27 * 19 = 513<\/pre>\n
given the first number = 23\r\ngiven the second number = 38<\/pre>\n
The product of the given numbers 23 and 38 is 23 * 38 = 874<\/pre>\n
Program to Find the Product of two Numbers Using Recursion<\/h2>\n
\n
1)Using Recursion(Static Input)<\/h3>\n
\n
# function who calculates the product of the given two numbers recursively\r\ndef productRecursion(firstnumb, secondnumb):\r\n # Give the base condition that if the first number is less than the second,\r\n # execute the method recursively with the numbers swapped.\r\n if(firstnumb < secondnumb):\r\n return productRecursion(secondnumb, firstnumb)\r\n # If the second number is not equal to 0, call the\r\n # method again recursively otherwise, return 0.\r\n elif(secondnumb != 0):\r\n return(firstnumb + productRecursion(firstnumb, secondnumb-1))\r\n else:\r\n return 0\r\n\r\n\r\n# Give the two numbers as static input and store them as two variables.\r\nfirstnumb = 27\r\nsecondnumb = 19\r\n# passing the given two numbers as arguments to the recursive function\r\n# which returns the product of the given two numbers\r\n# printing th product of th two numbers\r\nprint(\"The product of the given numbers\", firstnumb, 'and', secondnumb, 'is ' +\r\n str(firstnumb)+' * '+str(secondnumb)+' = ', productRecursion(firstnumb, secondnumb))\r\n<\/pre>\n
The product of the given numbers 27 and 19 is 27 * 19 = 513<\/pre>\n
2)Using Recursion(User Input separated by spaces)<\/h3>\n
\n
# function who calculates the product of the given two numbers recursively\r\ndef productRecursion(firstnumb, secondnumb):\r\n # Give the base condition that if the first number is less than the second,\r\n # execute the method recursively with the numbers swapped.\r\n if(firstnumb < secondnumb):\r\n return productRecursion(secondnumb, firstnumb)\r\n # If the second number is not equal to 0, call the\r\n # method again recursively otherwise, return 0.\r\n elif(secondnumb != 0):\r\n return(firstnumb + productRecursion(firstnumb, secondnumb-1))\r\n else:\r\n return 0\r\n\r\n\r\n# Give the two numbers as user input using the map() and split() function\r\n# and store them in two separate variables.\r\nfirstnumb, secondnumb = map(int, input(\r\n 'Enter some random numbers separated by spaces = ').split())\r\n# passing the given two numbers as arguments to the recursive function\r\n# which returns the product of the given two numbers\r\n# printing th product of th two numbers\r\nprint(\"The product of the given numbers\", firstnumb, 'and', secondnumb, 'is ' +\r\n str(firstnumb)+' * '+str(secondnumb)+' = ', productRecursion(firstnumb, secondnumb))\r\n<\/pre>\n
Enter some random numbers separated by spaces = 23 38\r\nThe product of the given numbers 23 and 38 is 23 * 38 = 874\r\n\r\n<\/pre>\n
\n
\n