 # C Programming Code Examples

## C > Functions Code Examples

### find sum of even or odd number in given range using recursion

``` find sum of even or odd number in given range using recursion Write a recursive function in C to find sum of all even or odd numbers in a given range. How to find sum of all even numbers between 1 to n using recursion in C programming. Declare recursive function to find sum of even number First give a meaningful name to our function, say sumOfEvenOdd(). Next the function accepts two integer values from user i.e. start and end range. Hence, update function declaration to sumOfEvenOdd(int start, int end);. Finally, after calculating sum of even or odd numbers the function must return it to the caller. Hence, return type of the function must be int. Final function declaration to find sum of all even or odd numbers in given range is - int sumOfEvenOdd(int start, int end);. #include <stdio.h> int sumOfEvenOdd(int start, int end); int main() { int start, end, sum; /* Input lower and upper limit from user */ printf("Enter lower limit: "); scanf("%d", &start); printf("Enter upper limit: "); scanf("%d", &end); printf("Sum of even/odd numbers between %d to %d = %d\n", start, end, sumOfEvenOdd(start, end)); return 0; } int sumOfEvenOdd(int start, int end) { /* Base condition */ if(start > end) return 0; else return (start + sumOfEvenOdd(start + 2, end)); } ``` 