Write a recursive function in C programming to print all even or odd numbers between 1 to n. How to print all even numbers in given range using recursion in C programming. Logic to print even/odd numbers in given range using recursion.

**Example**

**Input**

Input lower limit: 1 Input upper limit: 10

**Output**

Even numbers between 1 to 10: 2, 4, 6, 8, 10 Odd numbers between 1 to 10: 1, 3, 5, 7, 9

## Required knowledge

Basic C programming, If else, Functions, Recursion

Must know -

## Declare recursive function to print all even numbers

- First give a meaningful name to the recursive function to print even odd numbers. Let's say
`printEvenOdd()`

. This function can print both even as well as odd numbers in given range. - Next the function must accept two inputs i.e. the current number to print and the upper limit. Hence, update the function declaration to
`printEvenOdd(int cur, int limit);`

. - Finally, the function prints all even or odd numbers in given range and returns
`void`

.

So, the final function declaration to print even or odd numbers is - `void printEvenOdd(int cur, int limit);`

.

## Logic to print even numbers using recursion

Printing either even or odd numbers have same logic. Starting from a seed value increment the current number by 2 to get next value. When the current number exceeds the upper limit to print then terminate from function. Which is our required base condition to exit control from function. If the current number is less than upper limit than print the current number and recursively call the `printEvenOdd()`

with a new value of `cur` i.e. `printEvenOdd(cur + 2, limit);`

.

## Program to print even or odd numbers in given range using recursion

```
/**
* C program to print even or odd numbers in given range using recursion
*/
#include <stdio.h>
/* Function declaration */
void printEvenOdd(int cur, int limit);
int main()
{
int lowerLimit, upperLimit;
// Input lower and upper limit from user
printf("Enter lower limit: ");
scanf("%d", &lowerLimit);
printf("Enter upper limit: ");
scanf("%d", &upperLimit);
printf("Even/odd Numbers from %d to %d are: ", lowerLimit, upperLimit);
printEvenOdd(lowerLimit, upperLimit);
return 0;
}
/**
* Recursive function to print even or odd numbers in a given range.
*/
void printEvenOdd(int cur, int limit)
{
if(cur > limit)
return;
printf("%d, ", cur);
// Recursively call to printEvenOdd to get next value
printEvenOdd(cur + 2, limit);
}
```

Read more - Program to find sum of even numbers in given range using recursion

Output

Enter lower limit: 1 Enter upper limit: 100 Even/odd Numbers from 1 to 100 are: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99,

Happy coding ðŸ˜‰

### Recommended posts

- Function and recursion programming exercise index.
- C program to print all natural numbers in given range using recursion.
- C program to print sum of all natural numbers using recursion.
- C program to find sum of digits of a number using recursion.
- C program to find factorial of a number using recursion.
- C program to generate nth Fibonacci series using recursion.

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>