Question

[27] Write a recursive function to calculate the following for a given input value for x. result = 1+ 2+ 3 ..... + x For exam

(for python)

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

The code in python is given below.

def serialAdd(x):
if x<=1:
return x
else:
return(x+serialAdd(x-1))

n=int(input("Please Enter a Number: "))
print("The result is:",serialAdd(n))



The screenshot of the running code is given below.

1 - def serialAdd(x): 2 if x<=1: 3 return x 4 - else: 5 return(x+serialAdd(x-1)) 6 7 n=int(input(Please Enter a Number: ))




Output for n=5 then result is 15
n=10 then result is 55





If the answer helped please upvote it means a lot. For any query please comment.

Add a comment
Know the answer?
Add Answer to:
(for python) [27] Write a recursive function to calculate the following for a given input value...
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 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.

  • ****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,...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case....

    CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120 1 test passed 4 6 All tests 1 passed 8 9 1 def factorial_str(fact_counter, fact_value): 2 output_string = 3 if fact_counter == 0: # Base case: 0! = 1 5 output_string += '1' elif fact counter == 1: # Base case: print 1 and result 7...

  • Python Please Write a recursive function rPatt that, when given a power of 2 as an...

    Python Please Write a recursive function rPatt that, when given a power of 2 as an argument, returns the patterns indicated in the examples. OutPut: >>> rPatt(1) '|' >>> rPatt(2) '|--|' >>> rPatt(4) '|--|----|--|' >>> rPatt(8) '|--|----|--|--------|--|----|--|' >>> rPatt(16) '|--|----|--|--------|--|----|--|----------------|--|----|--|--------|--|----|--|' >>> rPatt(32) '|--|----|--|--------|--|----|--|----------------|--|----|--|--------|--|----|--|--------------------------------|--|----|--|--------|--|----|--|----------------|--|----|--|--------|--|----|--|' >>>

  • Write a function in python, index(arr, value) to find indices of elements equal to some value...

    Write a function in python, index(arr, value) to find indices of elements equal to some value in a Numpy array. The input arr is a 1-d numpy array, and input value is the value to search for. The function should return the index of the value. If the value occurs for multiple times, then all the indices should be returned as a 1-d numpy array. For example, if arr=[1 0 2 0 3 0 4 0 5 0 6 7...

  • Write a recursive function moreFactors(a,b,fact) that does the following: 1. Takes as an input 3 positive...

    Write a recursive function moreFactors(a,b,fact) that does the following: 1. Takes as an input 3 positive integers. (1 pts) 2. Of the two integers a and b, the function returns the integer that has the most factors fact. (5 pts) 3. If both integers a and b have the same amount of factors fact, the function will return the larger integer. (2 pts) Test your function with the following: (2 pts) >> result=moreFactors(24,32,3) result = 24 (24 = 31 23,...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

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

  • (Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to...

    (Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to solve the problem. void Triangle(int); Write a main function to test it. For example, a function call Triangle(6) will displays the following 6 rows of a triangle pattern. 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1...

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