 # C Programming Code Examples

## C > Functions Code Examples

### C 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 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. 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);. 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);. #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; } 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); } ``` 