Given two integers L and R, the task is to print all the even and odd numbers from L to R using recursion.
Input: L = 1, R = 10
Even numbers: 2 4 6 8 10
Odd numbers: 1 3 5 7 9
Input: L = 10, R = 25
Even numbers:10 12 14 16 18 20 22 24
Odd numbers:11 13 15 17 19 21 23 25
Approach: Follow the steps below to solve the problem using Recursion:
- Traverse the range [R, L].
- Print the odd elements from the range using recursion using the following recurrence relation:
Odd(L, R) = R % 2 == 1? Odd(L, R – 2) : Odd(L, R – 1)
- Print the even elements from the range using recursion using the following recurrence relation:
Even(L, R) = R % 2 == 0 ? Even(L, R – 2) : Even(L, R – 1)
Below is the implementation of the above approach:
Even numbers:10 12 14 16 18 20 22 24 Odd numbers:11 13 15 17 19 21 23 25
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Check whether XOR of all numbers in a given range is even or odd
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count Odd and Even numbers in a range from L to R
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Sum of range in a series of first odd then even natural numbers
- Generate an array of given size with equal count and sum of odd and even numbers
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Sum of even elements of an Array using Recursion
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Average of odd numbers till a given odd number
- Average of even numbers till a given even number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.