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

Write a recursive function in C programming to find the sum of all even or odd numbers in a given range. How to find the sum of all even numbers between 1 to n using recursion in C programming. How to find sum of all odd numbers between 1 to n using recursion in C programming. Logic to find sum of even numbers in given range using recursion in C programming.

Example

Input

```Input lower limit: 1
Input upper limit: 100```

Output

`Sum of even numbers between 1 to 100 = 2550`

## Required knowledge

Basic C programming, Function, Recursion

Finding sum of even or odd numbers in range is almost similar to the previous program we did.

Read more - Program to find sum of natural numbers

## Declare recursive function to find sum of even number

1. First give a meaningful name to the function, say sumOfEvenOdd().
2. Next the function accepts two integer values from user i.e. start and end range. Hence, change function declaration to accept two integer values from user i.e. sumOfEvenOdd(int start, int end);.
3. Finally, after calculating sum of even or odd numbers the function must return it to the caller. Hence, the return type of the function must be int.

Recursive function declaration to find sum of all even or odd numbers in given range - int sumOfEvenOdd(int start, int end);.

## Logic to find sum of even or odd numbers using recursion

Above is the mathematical definition of function to find sum of even or odd number recursively. To find sum of even or odd number we define a function called int sumOfNaturalNumbers(int start, int end);. Inside function if start > end then return 0 which is our base condition. if(start <= end) then recursively call the sumOfNaturalNumbers() function to get the sum of even numbers i.e. start + sumOfNaturalNumbers(start + 2, end);.

## Program to find sum of even or odd numbers using recursion

``````/**
* C program to find sum of all even or odd numbers in given range using recursion
*/

#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;
}

/**
* Find sum of all even or odd numbers recursively.
*/
int sumOfEvenOdd(int start, int end)
{
if(start <= end)
return (start + sumOfEvenOdd(start + 2, end));
else
return 0;
}``````

Output

```Enter lower limit: 2
Enter upper limit: 100
Sum of even/odd numbers between 2 to 100 = 2550```

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside