Question

****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, 3, 5, 6], count_odd([2, 3, 5, 6])))
[2, 3, 5, 6] : 2

Q3.

Write a recursive Python function, given a non-negative integer N, to calculate and return 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 may not use loops! This function takes in one integer and returns one integer.

For example:

Test Result
print(234, ":", sum_digits(234))
234 : 9

Q4.

Write a recursive function to compute the following series:

sumseries1

This function has to be recursive; you may not use loops!

For example:

Test Result
print('%d : %.6f' %( 2,  m(2)))
2 : 1.500000

Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Question 1:

#function that return sum
def sum_up_to(num):
#checking number
if(num<=1):
#if number equal to 1 then
return num
else:
#recursively call the function
return num+sum_up_to(num-1)
  
#call function
print('%d : %d' % (5, sum_up_to(5)))

======================================================

Output : Compile and Run above function to get the screen as shown below

Screen 1 :main.py

main.py 1 #function that return sum 2 def sum_up__to(num): #checking number if (num-1): #if number equal to 1 then return num

*************************

Question 3:

#function to find out sum of digits
def sum_digits(num):
#checking number
if num==0:
#if num is equal to zero
return 0
else:
#call recursively sum_digits
return num%10+sum_digits(num//10)
#function call
print(234, ":", sum_digits(234))

===================================

Output :

main.py 1 #function to find out sum of digits 2 def sum_digits(num): #checking number if num--0: 4 #if num is equal to zero 5

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...
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
  • 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...

  • 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 called freq_of(letter, text) that finds the number of occurrences of a specified...

    Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string. This function has to be recursive; you may not use loops!   CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them. For example: Test Result text = 'welcome' letter = 'e' result = freq_of(letter, text) print(f'{text} : {result} {letter}') welcome : 2 e and A list can be...

  • 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

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Write a recursive function Programming: to_number that forms the integer sum of all digit characters in...

    Write a recursive function Programming: to_number that forms the integer sum of all digit characters in a string. For example, the result of to_number("3ac4") would be 7. C++ i know this has been answered before but I keep getting 0 when I enter a string.

  • Write a recursive function sum-odds that takes a non-empty list of integers

    in python Part I: Sum of Odd Integers Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops

  • (for python) [27] Write a recursive function to calculate the following for a given input value...

    (for python) [27] Write a recursive function to calculate the following for a given input value for x. result = 1+ 2+ 3 ..... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.

  • 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)....

  • python language. [27] Write a recursive function to calculate the following for a given input value...

    python language. [27] Write a recursive function to calculate the following for a given input value for x. result = 1 + 2 + 3 .... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.

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