Question

PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

PYTHON:

(Sum the digits in an integer using recursion)

Write a recursive function that computes the sum of the digits in an integer. Use the following function header:

def sumDigits(n):

For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits.

Sample Run

Enter an integer: 231498

The sum of digits in 231498 is 27

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def sumDigits(num):
    if(num <= 9):
        return num
    else:
        return (num%10) + sumDigits(num//10)

def main():
    n = eval(input("Enter an integer: "))
    print("The sum of digits in",n,"is",sumDigits(n))

main()

Add a comment
Know the answer?
Add Answer to:
PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Write a recursive function in Python to find the sum of digits of a number. Name...

    Write a recursive function in Python to find the sum of digits of a number. Name the function sum_of_digits. The function should use recursive algorithm (calling itself). The function should print out the sum of all the digits of a given number. For example, sum_of_digits(343) should have a output of 10. Marks will be deducted if you do not follow strictly to the instructions. [3]: N 1 def sum_of_digits(n): HNM in sum_of_digits (343) Out[3]: 10

  • need help with python ( no loops used please!) Write a recursive function named sum_digits that...

    need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...

  • In Python 3 (2.5 pts] Write the recursive function thirtyTwos(n) that takes an integer greater or...

    In Python 3 (2.5 pts] Write the recursive function thirtyTwos(n) that takes an integer greater or equal to 0 and returns an integer that represents the number of times that a 2 directly follows a 3 in the digits of n. Hint: The % and // operations from sumDigits could be helpful here >>> thirtyTwos (132432601)

  • Using PYTHON: (Find the index of the smallest element) Write a function that returns the index...

    Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!

  • In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

    In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • Please help me with this question Write a recursive method that returns the largest integer in...

    Please help me with this question Write a recursive method that returns the largest integer in an array. Write a test program that prompts the user to enter a list of eight integers and displays the largest largest element. Thank you! and can you give details so I know what is going on?

  • Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits...

    Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits in the integer n using a recursive process. For example, (digits 42) should return 2 and (digits 13579) should return 5. You may make use of the built in floor predicate for truncating decimals to whole numbers. b. Rewrite your procedure from part (a) using an iterative process. Call the function (digits-iter n).

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT