{"id":9571,"date":"2021-09-30T17:30:20","date_gmt":"2021-09-30T12:00:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=9571"},"modified":"2021-11-22T18:33:32","modified_gmt":"2021-11-22T13:03:32","slug":"python-program-to-find-the-sum-of-the-digits-of-the-number-recursively","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-the-digits-of-the-number-recursively\/","title":{"rendered":"Python Program to Find the Sum of the Digits of the Number Recursively"},"content":{"rendered":"
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/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 Given a number the task is to calculate the sum of the digits of the given number using recursive approach 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 calculate the sum of the digits of the given number using recursive approach in Python.<\/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 Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers. Recursion: Recursion is the process by which a function calls itself directly …<\/p>\nEnter some random number = 18627677851<\/pre>\n
The total sum of digits off the given number 18627677851 = 58<\/pre>\n
Enter some random number = 7816833887102099<\/pre>\n
The total sum of digits off the given number 7816833887102099 = 80<\/pre>\n
Program to Find the Sum of the Digits of the Number Recursively<\/h2>\n
\n
1)Using Recursion(Static Input)<\/h3>\n
\n
# take a empty list\r\nnumbList = []\r\n# function which returns count of all the digits of\r\n# the given number using recursive approach.\r\n\r\n\r\ndef sumDigitsRecursion(numb):\r\n # Put the base condition in the function that says\r\n # if the number is zero, return the created list.\r\n if(numb == 0):\r\n return numbList\r\n # getting the last digit of the given number using modulus operator\r\n numdigit = numb % 10\r\n # appending this digit to numberslist using digit function\r\n numbList.append(numdigit)\r\n # passing numb\/10 recursively\r\n sumDigitsRecursion(numb\/\/10)\r\n\r\n\r\n# give the number as static input\r\nnumb = 18627677851\r\n# passing the number to sumDigitsRecursion function to\r\n# calculate the sum of digits recursively\r\nsumDigitsRecursion(numb)\r\n# calculating the sum of list using sum() function.\r\nprint('The total sum of digits off the given number', numb, '=', sum(numbList))\r\n<\/pre>\n
The total sum of digits off the given number 18627677851 = 58<\/pre>\n
2)Using Recursion(User Input)<\/h3>\n
\n
# take a empty list\r\nnumbList = []\r\n# function which returns count of all the digits of\r\n# the given number using recursive approach.\r\n\r\n\r\ndef sumDigitsRecursion(numb):\r\n # Put the base condition in the function that says\r\n # if the number is zero, return the created list.\r\n if(numb == 0):\r\n return numbList\r\n # getting the last digit of the given number using modulus operator\r\n numdigit = numb % 10\r\n # appending this digit to numberslist using digit function\r\n numbList.append(numdigit)\r\n # passing numb\/10 recursively\r\n sumDigitsRecursion(numb\/\/10)\r\n\r\n\r\n#scan some random number using int(input()) function\r\nnumb = int(input('Enter some random number = '))\r\n# passing the number to sumDigitsRecursion function to\r\n# calculate the sum of digits recursively\r\nsumDigitsRecursion(numb)\r\n# calculating the sum of list using sum() function.\r\nprint('The total sum of digits off the given number', numb, '=', sum(numbList))\r\n<\/pre>\n
Enter some random number = 7816833887102099\r\nThe total sum of digits off the given number 7816833887102099 = 80<\/pre>\n
\n
\n