Question 5: 2.1 Sum Problem View Past Answers Given a positive number n, the sum of all digits is obtained by adding the digit one-by-one. For example, the sum of 52634 is 5+ 2 +6 +3 + 4 = 20. Write a...


Question 5: 2.1 Sum Problem<br>View Past Answers<br>Given a positive number n, the sum of all digits is obtained by adding the digit one-by-one. For example, the sum of 52634 is 5+ 2 +6 +3 + 4 = 20. Write a recursive and iterative function<br>sum (n) to compute the sum of all the digits in n. You may assume that n > 0.<br>template.py<br>1 def sum R(n):<br>if len(str(n)) == 1:<br>2-<br>return n<br>else:<br>return 20<br>7. def sum_I(n):<br>add = 0<br>8<br>while n:<br>10<br>add += n % 10<br>n //= 10<br>return add<br>11<br>12<br>

Extracted text: Question 5: 2.1 Sum Problem View Past Answers Given a positive number n, the sum of all digits is obtained by adding the digit one-by-one. For example, the sum of 52634 is 5+ 2 +6 +3 + 4 = 20. Write a recursive and iterative function sum (n) to compute the sum of all the digits in n. You may assume that n > 0. template.py 1 def sum R(n): if len(str(n)) == 1: 2- return n else: return 20 7. def sum_I(n): add = 0 8 while n: 10 add += n % 10 n //= 10 return add 11 12

Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here