Question

Informally describe a recursive algorithm to compute the sum of the digits of a positive integer.

Informally describe a recursive algorithm to compute the sum of the digits of a positive integer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
procedure sum_of_digits(num)
    total = 0
    while num >  0
        total = total + (num % 10)
        num = num / 10
    end while
    return total
end procedure
Add a comment
Know the answer?
Add Answer to:
Informally describe a recursive algorithm to compute the sum of the digits of a positive integer.
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: (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

  • write a recursive function that returns true if the digits of a positive integer are in...

    write a recursive function that returns true if the digits of a positive integer are in increasing order ; otherwise, the function returns false. Also write a program to test your function.

  • 1. We can determine how many digits a positive integer has by repeatedly dividing by 10...

    1. We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10. a) Describe the algorithm in a recursive way b) Implement this recursive algorithm in C++ and test it using a main function that calls this with the values of 15, 105 and 15105. (Hint: Remember...

  • 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

  • Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) =...

    Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + … + n3. (a) Set up a recurrence relation for the number of multiplications made by this algorithm. (b) Provide an initial condition for the recurrence relation you develop at the question (a). (c) Solve the recurrence relation of the question (a) and present the time complexity as described at the question number 1. Algorithm S n) Input: A positive...

  • Arrays,Lists, Stacks and queues 1) Describe a recursive function to convert a string of digits into...

    Arrays,Lists, Stacks and queues 1) Describe a recursive function to convert a string of digits into the integer it represents. For example, the string “3735928559” would be converted to the integer value 3,735,928,559.[Preferrably in C++]

  • C++: Write a recursive function that does the following: Given a number, add all the digits...

    C++: Write a recursive function that does the following: Given a number, add all the digits and display the sum. Example:                         The sum of the number 5432 would be 14. PLEASE PAY ATTENTION TO THE FOLLOWING: Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from...

  • 1. (Sum the digits in an integer) Write a method that computes the sum of the...

    1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....

  • Write a recursive function named arithmeticSum that takes a positive integer parameter n and returns the...

    Write a recursive function named arithmeticSum that takes a positive integer parameter n and returns the sum of the integer numbers from 1 to n Please write in C++

  • If you understand how the above recursive algorithm to compute yz works, you can turn it...

    If you understand how the above recursive algorithm to compute yz works, you can turn it into a more efficient iterative algorithm that basically uses the same strategy (though it is not a tail recursive algorithm). Some parts of this iterative algorithm is given below. Fill in the blanks: Power-iterative(y: number; z: non-negative integer) 1. answer=1 2. while z > 0 3.    if z is odd then answer=__________ 4.    z = ________ 5.    y = _________ 6....

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