Python: Recursion - Exercises, Practice, SolutionLast update on August 19 2022 21:50:47 (UTC/GMT +8 hours)Recursion [ 11 exercises with solution][An editor is available at the bottom of the page to write and execute the scripts.] Show
1. Write a Python program to calculate the sum of a list of
numbers. Go to the editor 2. Write a Python program to converting an Integer to a string in any base.
Go to the editor 3. Write a Python program of recursion list sum.
Go to the editor 4. Write a Python program to get the factorial of a non-negative integer.
Go to the editor 5. Write a Python program to solve the Fibonacci sequence using recursion.
Go to the editor 6. Write a Python program to get the sum of a non-negative integer.
Go to the editor 7. Write a Python program to calculate the sum of the positive
integers of n+(n-2)+(n-4)... (until n-x =< 0). Go to the editor 8. Write a Python
program to calculate the harmonic sum of n-1. Go to the editor Click me to see the sample solution 9. Write a Python program to calculate the geometric sum of n-1. Go to the editor Click me to see the sample solution 10. Write a Python program to calculate the value of 'a' to the power 'b'.
Go to the editor 11. Write a Python program to find the greatest common divisor (gcd) of two
integers. Go to the editor Python Code Editor: More to Come ! Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page. Test your Python skills with w3resource's quiz Python: Tips of the DayUnpack variables using the splat operator: >>> def test(x, y, z): >>> print(x, y, z) >>> res = test(*[10, 20, 30]) 10 20 30 >>> res = test(**{'x': 1, 'y': 2, 'z': 3} ) 10 20 30 We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Start Your Coding Journey Now!If you are new to Python and struggle to get your hands dirty with Recursive functions, you must try to solve the problems listed in this article. The article is sorted… Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding :) In programming, recursion is a technique using a function or an algorithm that calls itself one or more times until a particular condition is met. A recursive function is a function that calls itself with a failure condition. It means that there will be one or more function calls within that function definition itself.</p><p><span id="ezoic-pub-ad-placeholder-153" class="ezoic-adpicker-ad"></span>Let’s see how we can implement recursion using Python. In this article, I have provided a few examples of using recursion in Python. Check out these examples, and I hope they will help you get a clear idea about the concept of recursion in programming. Let’s dive right in.</p><h2>Example 1: Finding the factorial of a number</h2><p>If you are familiar with the concept of factorial in mathematics, this will be a simple example of recursion for you to start with. In mathematics, the equation for finding the factorial of a number is as follows:</p><pre class="wp-block-preformatted">n! = n * (n-1)!</pre><p>Given below is a Python program that finds out the factorial of a number by calling a function recursively.</p><pre class="wp-block-code"><code>def fact(n): if(n==1): return n else: return n*(fact(n-1)) num = int(input("Enter a number: ")) if num<0: print("Negative numbers are not allowed.") elif num==0: print("Factorial is: 1") else: print("Factorial is: ",fact(num)) </code></pre><figure class="wp-block-image size-full"><img width="285" height="78" alt="" class="ezlazyload wp-image-8173" ezimgfmt="rs rscb19 src ng ngcb19" data-ezsrc="https://pythonistaplanet.com/wp-content/uploads/2021/11/image.png"></figure><h2>Example 2: Fibonacci Series</h2><p>Fibonacci is a special kind of series in mathematics in which the current term is the sum of the previous two terms. For example, a <a href="https://pythonistaplanet.com/fibonacci-sequence-iterative/" data-type="URL" data-id="https://pythonistaplanet.com/fibonacci-sequence-iterative/" target="_blank" rel="noreferrer noopener">Fibonacci series</a> would look like this:</p><pre class="wp-block-preformatted">0, 1, 1, 2, 3, 5, 8, 13,…</pre><p><span id="ezoic-pub-ad-placeholder-164" class="ezoic-adpicker-ad"></span><span class="ezoic-ad ezoic-at-0 medrectangle-4 medrectangle-4164 adtester-container adtester-container-164" data-ez-name="pythonistaplanet_com-medrectangle-4"><span id="div-gpt-ad-pythonistaplanet_com-medrectangle-4-0" ezaw="300" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;min-height:250px;min-width:300px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" style="display:none">if(typeof ez_ad_units!='undefined'){ez_ad_units.push([[300,250],'pythonistaplanet_com-medrectangle-4','ezslot_7',164,'0','0'])};__ez_fad_position('div-gpt-ad-pythonistaplanet_com-medrectangle-4-0');In this series, the next number is found by adding the two numbers before that. Given below is a Python code that finds out the Fibonacci series till a number n.
Example 3: Finding the power of a numberThe mathematical equation for finding the power of a number is as follows: p(base, exp) = base * p(b, exp-1)
|