{"id":20224,"date":"2021-09-09T19:57:33","date_gmt":"2021-09-09T14:27:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=20224"},"modified":"2021-11-22T18:36:24","modified_gmt":"2021-11-22T13:06:24","slug":"python-program-to-find-sum-of-two-numbers-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-two-numbers-using-recursion\/","title":{"rendered":"Python Program to Find Sum of Two Numbers using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the First Small Letter in a Given String<\/a><\/p>\n Given two numbers and the task is to find the sum 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 sum 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 Practice Python Program Examples<\/a> to master coding skills and learn the fundamental concepts in the dynamic programming language Python.<\/p>\n In the previous article, we have discussed Python Program to Find the First Small Letter in a Given String Given two numbers and the task is to find the sum 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 …<\/p>\nGiven First Number = 3\r\nGiven Second Number = 0<\/pre>\n
The sum of { 3 } and { 0 } using recursion = 3<\/pre>\n
Given First Number = 20\r\nGiven Second Number = 30<\/pre>\n
The sum of { 20 } and { 30 } using recursion = 50<\/pre>\n
Program to Find Sum of Two Numbers using Recursion in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say recur_sum which takes the two numbers as arguments\r\n# and returns the sum of the given two numbers using recursion.\r\n\r\n\r\ndef recur_sum(fst_numb, secnd_numb):\r\n # Check if the second number is equal to 0 using the if conditional statement.\r\n if secnd_numb == 0:\r\n # If the statement is true, return the first number.\r\n return fst_numb\r\n # Else return (fst_numb, secnd_numb-1)+1 {Recursive logic}\r\n return recur_sum(fst_numb, secnd_numb-1)+1\r\n\r\n\r\n# Give the first number as static input and store it in a variable.\r\nfst_numb = 3\r\n# Give the second number as static input and store it in another variable.\r\nsecnd_numb = 0\r\n#Pass the given two numbers as the arguments to recur_sum function\r\nprint(\"The sum of {\", fst_numb, \"} and {\", secnd_numb,\r\n \"} using recursion =\", recur_sum(fst_numb, secnd_numb))\r\n<\/pre>\n
The sum of { 3 } and { 0 } using recursion = 3<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say recur_sum which takes the two numbers as arguments\r\n# and returns the sum of the given two numbers using recursion.\r\n\r\n\r\ndef recur_sum(fst_numb, secnd_numb):\r\n # Check if the second number is equal to 0 using the if conditional statement.\r\n if secnd_numb == 0:\r\n # If the statement is true, return the first number.\r\n return fst_numb\r\n # Else return (fst_numb, secnd_numb-1)+1 {Recursive logic}\r\n return recur_sum(fst_numb, secnd_numb-1)+1\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_sum function\r\nprint(\"The sum of {\", fst_numb, \"} and {\", secnd_numb,\r\n \"} using recursion =\", recur_sum(fst_numb, secnd_numb))\r\n<\/pre>\n
Enter some random number = 20\r\nEnter some random number = 30\r\nThe sum of { 20 } and { 30 } using recursion = 50<\/pre>\n
\n