{"id":10017,"date":"2021-09-30T17:30:51","date_gmt":"2021-09-30T12:00:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=10017"},"modified":"2021-11-22T18:33:31","modified_gmt":"2021-11-22T13:03:31","slug":"python-program-to-find-the-lcm-of-two-numbers-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-lcm-of-two-numbers-using-recursion\/","title":{"rendered":"Python Program to Find the LCM of Two Numbers Using Recursion"},"content":{"rendered":"
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners<\/a> tutorial.<\/p>\n Recursion:<\/strong><\/p>\n Recursion is a functional technique to problem-solving that involves breaking an issue down into a series of small subproblems with an identical pattern and solving them sequentially by calling one subproblem within another. Recursion is carried out by defining a function capable of solving one subproblem at a time. It calls itself but solves another subproblem somewhere inside that method. As a result, the call to itself continues until some limiting requirements are met.<\/p>\n The main program’s first call to a recursive function will be returned only after all sub calls have completed. As a result, Python keeps the results of all subproblems in temporary memory, does some arithmetic operations (if necessary), and releases the memory at the end of the recursion.<\/p>\n The smallest positive number that is divisible by both a and b is the least or lowest common multiple (LCM) of two numbers a and b. In this article, we’ll look at how to use recursion to find the LCM of two numbers.<\/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 There are several ways to calculate the lcm of the given two numbers using recursion some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial. Recursion: Recursion is a functional technique to problem-solving that involves breaking an issue down into a series of small subproblems with an identical pattern and solving them sequentially by calling one subproblem within another. …<\/p>\ngiven first number = 10\r\ngiven second number = 16<\/pre>\n
The value of lcm of the given two numbers 10 16 = 80<\/pre>\n
given first number = 10\r\ngiven second number = 16<\/pre>\n
The value of lcm of the given two numbers 5 17 = 85<\/pre>\n
Program to Find the LCM of Two Numbers Using Recursion in Python<\/h2>\n
\n
Method #1:Using Recursion (Static Input)<\/h3>\n
\n
# function which accepts the given two numbers as arguments\r\n# and return the lcm of the given two numbers.\r\n\r\n\r\ndef lcmRecursion(numb1, numb2):\r\n # Set the multiple variable to the highest value between the given two numbers.\r\n lcmRecursion.multiple = lcmRecursion.multiple+numb2\r\n # Check to see if the multiple variable\u00a0 divides both given numbers perfectly.\r\n if((lcmRecursion.multiple % numb1 == 0) and (lcmRecursion.multiple % numb2 == 0)):\r\n # If it does, the process is terminated and the multiple variable is returned as the LCM.\r\n return lcmRecursion.multiple\r\n else:\r\n # If the multiple does not divide both given numbers perfectly, then it is increased by the maximum value\r\n # among both given numbers using the max() function.\r\n lcmRecursion(numb1, numb2)\r\n # The recursive function returns the multiple variable\r\n # which is the result(lcm of the given two numbers)\r\n return lcmRecursion.multiple\r\n\r\n\r\n# set multiplee variable value to 0\r\nlcmRecursion.multiple = 0\r\n# Give two numbers as static input and store them in two variables.\r\nnumbe1 = 10\r\nnumbe2 = 16\r\n# if the first number is greater than second number then swap the arguments\r\nif(numbe1 > numbe2):\r\n LcmVal = lcmRecursion(numbe2, numbe1)\r\n# else pass the original numbers as arguments\r\nelse:\r\n LcmVal = lcmRecursion(numbe1, numbe2)\r\nprint('The value of lcm of the given two numbers', numbe1, numbe2, ' = ', LcmVal)\r\n<\/pre>\n
The value of lcm of the given two numbers 10 16 = 80<\/pre>\n
\n
Method #2:Using Recursion (User Input separated by space)<\/h3>\n
\n
# function which accepts the given two numbers as arguments\r\n# and return the lcm of the given two numbers.\r\n\r\n\r\ndef lcmRecursion(numb1, numb2):\r\n # Set the multiple variable to the highest value between the given two numbers.\r\n lcmRecursion.multiple = lcmRecursion.multiple+numb2\r\n # Check to see if the multiple variable\u00a0 divides both given numbers perfectly.\r\n if((lcmRecursion.multiple % numb1 == 0) and (lcmRecursion.multiple % numb2 == 0)):\r\n # If it does, the process is terminated and the multiple variable is returned as the LCM.\r\n return lcmRecursion.multiple\r\n else:\r\n # If the multiple does not divide both given numbers perfectly, then it is increased by the maximum value\r\n # among both given numbers using the max() function.\r\n lcmRecursion(numb1, numb2)\r\n # The recursive function returns the multiple variable\r\n # which is the result(lcm of the given two numbers)\r\n return lcmRecursion.multiple\r\n\r\n\r\n# set multiplee variable value to 0\r\nlcmRecursion.multiple = 0\r\n# Scan the given two numbers as user input using a map,\r\n# int, and split() functions and store them in two variables.\r\nnumbe1, numbe2 = map(int, input(\r\n 'Enter two random numbers separated by space =').split())\r\n# if the first number is greater than second number then swap the arguments\r\nif(numbe1 > numbe2):\r\n LcmVal = lcmRecursion(numbe2, numbe1)\r\n# else pass the original numbers as arguments\r\nelse:\r\n LcmVal = lcmRecursion(numbe1, numbe2)\r\nprint('The value of lcm of the given two numbers', numbe1, numbe2, ' = ', LcmVal)\r\n<\/pre>\n
Enter two random numbers separated by space =5 17\r\nThe value of lcm of the given two numbers 5 17 = 85\r\n<\/pre>\n
Here the map function converts the given two numbers from string to integer data type and the split() function\r\nsplits the given two numbers by space.<\/pre>\n
Method #3:Using Recursion (User Input separated by newline)<\/h3>\n
\n
# function which accepts the given two numbers as arguments\r\n# and return the lcm of the given two numbers.\r\n\r\n\r\ndef lcmRecursion(numb1, numb2):\r\n # Set the multiple variable to the highest value between the given two numbers.\r\n lcmRecursion.multiple = lcmRecursion.multiple+numb2\r\n # Check to see if the multiple variable\u00a0 divides both given numbers perfectly.\r\n if((lcmRecursion.multiple % numb1 == 0) and (lcmRecursion.multiple % numb2 == 0)):\r\n # If it does, the process is terminated and the multiple variable is returned as the LCM.\r\n return lcmRecursion.multiple\r\n else:\r\n # If the multiple does not divide both given numbers perfectly, then it is increased by the maximum value\r\n # among both given numbers using the max() function.\r\n lcmRecursion(numb1, numb2)\r\n # The recursive function returns the multiple variable\r\n # which is the result(lcm of the given two numbers)\r\n return lcmRecursion.multiple\r\n\r\n\r\n# set multiplee variable value to 0\r\nlcmRecursion.multiple = 0\r\n# Scan the first number as user input using the int(input())\r\n# function and store it in a\u00a0 variable.\r\nnumbe1 = int(input('Enter some random number = '))\r\n# Scan the second number as user input using the int(input())\r\n# function and store it in another variable.\r\n# Here int() is used to convert the given number to integer datatype.\r\nnumbe2=int(input('Enter some random number = '))\r\n# if the first number is greater than second number then swap the arguments\r\nif(numbe1 > numbe2):\r\n LcmVal=lcmRecursion(numbe2, numbe1)\r\n# else pass the original numbers as arguments\r\nelse:\r\n LcmVal=lcmRecursion(numbe1, numbe2)\r\nprint('The value of lcm of the given two numbers', numbe1, numbe2, ' = ', LcmVal)\r\n<\/pre>\n
Enter some random number = 9\r\nEnter some random number = 3\r\nThe value of lcm of the given two numbers 9 3 = 9<\/pre>\n
\n