# Count Even and Odd numbers in a range

Counting even and odd numbers in a range involves determining the number of integers within a specified range that are either even or odd. An even number is a number that is divisible by 2 without leaving any remainder, while an odd number is a number that is not divisible by 2 without leaving a remainder.

To count even and odd numbers in a range, you can iterate over all the integers in the range and check whether each integer is even or odd. If the integer is even, increment a counter for even numbers. Similarly, if the integer is odd, increment a counter for odd numbers.

Here's an example code snippet in Python that counts even and odd numbers in a range from 1 to 10:

``````start = 1
end = 10
even_count = 0
odd_count = 0

for num in range(start, end+1):
if num % 2 == 0:
even_count += 1
else:
odd_count += 1

print("Number of even numbers:", even_count)
print("Number of odd numbers:", odd_count)
``````

This code outputs:

``````Number of even numbers: 5
Number of odd numbers: 5``````

Better solution

To count even and odd numbers in a range without using loop or recursion, you can use the following formula:

• Count of even numbers = (range_end // 2) - (range_start - 1) // 2
• Count of odd numbers = (range_end - range_start + 1) - count of even numbers

Here, the "//" operator is used for integer division.

Explanation: To count the number of even numbers in the range, we can divide the range into two halves: the first half contains all even numbers and the second half contains all odd numbers. The number of even numbers in a range can be calculated by counting the number of integers in the first half.

The first half of the range starts at the first even number, which is the smallest even number greater than or equal to the range start. To find the first even number, we can divide the range start by 2 and round up to the nearest integer (using integer division). Similarly, the second half of the range starts at the first odd number, which is the smallest odd number greater than or equal to the range start. To find the first odd number, we can add 1 to the first even number.

Similarly, to count the number of odd numbers in the range, we can subtract the count of even numbers from the total number of integers in the range.

Note that this formula works only for non-negative integers. If the range includes negative integers, the formula needs to be modified accordingly.

## Code Solution

We can also calculate the even and odd number efficiently using new approach.

``````// C program
// Count Even and Odd numbers in a range
#include <stdio.h>

//Count the number of Even and Odd elements in given range
void count_even_odd(int first, int last)
{
if (first > last)
{
//Transform into a valid range
count_even_odd(last, first);
}
else
{
int even = 0;
int odd = 0;
//Count number of element
int length = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = (length / 2) + 1;
}
else
{
even = length / 2;
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = (length / 2) + 1;
}
else
{
odd = length / 2;
}
even = length - odd;
}
printf("\n Range [%d,%d] : Even [%d], Odd [%d] ", first, last, even,odd);
}
}
int main()
{
//Test case
count_even_odd(2, 8);
count_even_odd(1, 10);
count_even_odd(21, 33);
count_even_odd(7, 29);
count_even_odd(0, 0);
return 0;
}``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````/*
Java program
Count Even and Odd numbers in a range
*/
class EvenOddNumber
{
//Count the number of Even and Odd elements in given range
public void count_even_odd(int first, int last)
{
if (first > last)
{
//Transform into a valid range
count_even_odd(last, first);
}
else
{
int even = 0;
int odd = 0;
//Count number of element
int length = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = (length / 2) + 1;
}
else
{
even = length / 2;
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = (length / 2) + 1;
}
else
{
odd = length / 2;
}
even = length - odd;
}
System.out.print("\n Range [" + first + "," + last + "] : Even [" + even + "], Odd [" + odd + "] ");
}
}
public static void main(String[] args)
{
EvenOddNumber obj = new EvenOddNumber();
//Test case
obj.count_even_odd(2, 8);
obj.count_even_odd(1, 10);
obj.count_even_odd(21, 33);
obj.count_even_odd(7, 29);
obj.count_even_odd(0, 0);
}
}``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````//Include header file
#include <iostream>
using namespace std;

/*
C++ program
Count Even and Odd numbers in a range
*/

class EvenOddNumber
{
public:
//Count the number of Even and Odd elements in given range
void count_even_odd(int first, int last)
{
if (first > last)
{
//Transform into a valid range
this->count_even_odd(last, first);
}
else
{
int even = 0;
int odd = 0;
//Count number of element
int length = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = (length / 2) + 1;
}
else
{
even = length / 2;
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = (length / 2) + 1;
}
else
{
odd = length / 2;
}
even = length - odd;
}
cout << "\n Range [" << first << "," << last << "] : Even [" << even << "], Odd [" << odd << "] ";
}
}
};
int main()
{
EvenOddNumber obj = EvenOddNumber();
//Test case
obj.count_even_odd(2, 8);
obj.count_even_odd(1, 10);
obj.count_even_odd(21, 33);
obj.count_even_odd(7, 29);
obj.count_even_odd(0, 0);
return 0;
}``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````//Include namespace system
using System;

/*
C# program
Count Even and Odd numbers in a range
*/

class EvenOddNumber
{
//Count the number of Even and Odd elements in given range
public void count_even_odd(int first, int last)
{
if (first > last)
{
//Transform into a valid range
count_even_odd(last, first);
}
else
{
int even = 0;
int odd = 0;
//Count number of element
int length = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = (length / 2) + 1;
}
else
{
even = length / 2;
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = (length / 2) + 1;
}
else
{
odd = length / 2;
}
even = length - odd;
}
Console.Write("\n Range [" + first + "," + last + "] : Even [" + even + "], Odd [" + odd + "] ");
}
}
public static void Main(String[] args)
{
EvenOddNumber obj = new EvenOddNumber();
//Test case
obj.count_even_odd(2, 8);
obj.count_even_odd(1, 10);
obj.count_even_odd(21, 33);
obj.count_even_odd(7, 29);
obj.count_even_odd(0, 0);
}
}``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````<?php
/*
Php program
Count Even and Odd numbers in a range
*/
class EvenOddNumber
{
//Count the number of Even and Odd elements in given range
public	function count_even_odd(\$first, \$last)
{
if (\$first > \$last)
{
//Transform into a valid range
\$this->count_even_odd(\$last, \$first);
}
else
{
\$even = 0;
\$odd = 0;
//Count number of element
\$length = (\$last - \$first) + 1;
if (\$first % 2 == 0)
{
if (\$last % 2 == 0)
{
//When both given number is even
\$even = (intval(\$length / 2)) + 1;
}
else
{
\$even = intval(\$length / 2);
}
\$odd = \$length - \$even;
}
else
{
if (\$last % 2 != 0)
{
\$odd = (intval(\$length / 2)) + 1;
}
else
{
\$odd = intval(\$length / 2);
}
\$even = \$length - \$odd;
}
echo "\n Range [". \$first .",". \$last ."] : Even [". \$even ."], Odd [". \$odd ."] ";
}
}
}

function main()
{
\$obj = new EvenOddNumber();
//Test case
\$obj->count_even_odd(2, 8);
\$obj->count_even_odd(1, 10);
\$obj->count_even_odd(21, 33);
\$obj->count_even_odd(7, 29);
\$obj->count_even_odd(0, 0);
}
main();``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````/*
Node Js program
Count Even and Odd numbers in a range
*/
class EvenOddNumber
{
//Count the number of Even and Odd elements in given range
count_even_odd(first, last)
{
if (first > last)
{
//Transform into a valid range
this.count_even_odd(last, first);
}
else
{
var even = 0;
var odd = 0;
//Count number of element
var length = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = (parseInt(length / 2)) + 1;
}
else
{
even = parseInt(length / 2);
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = (parseInt(length / 2)) + 1;
}
else
{
odd = parseInt(length / 2);
}
even = length - odd;
}
process.stdout.write("\n Range [" + first + "," + last + "] : Even [" + even + "], Odd [" + odd + "] ");
}
}
}

function main()
{
var obj = new EvenOddNumber();
//Test case
obj.count_even_odd(2, 8);
obj.count_even_odd(1, 10);
obj.count_even_odd(21, 33);
obj.count_even_odd(7, 29);
obj.count_even_odd(0, 0);
}
main();``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````#   Python 3 program
#   Count Even and Odd numbers in a range

class EvenOddNumber :
# Count the number of Even and Odd elements in given range
def count_even_odd(self, first, last) :
if (first > last) :
# Transform into a valid range
self.count_even_odd(last, first)
else :
even = 0
odd = 0
# Count number of element
length = (last - first) + 1
if (first % 2 == 0) :
if (last % 2 == 0) :
# When both given number is even
even = (int(length / 2)) + 1
else :
even = int(length / 2)

odd = length - even
else :
if (last % 2 != 0) :
odd = (int(length / 2)) + 1
else :
odd = int(length / 2)

even = length - odd

print("\n Range [", first ,",", last ,"] : Even [", even ,"], Odd [", odd ,"] ", end = "")

def main() :
obj = EvenOddNumber()
# Test case
obj.count_even_odd(2, 8)
obj.count_even_odd(1, 10)
obj.count_even_odd(21, 33)
obj.count_even_odd(7, 29)
obj.count_even_odd(0, 0)

if __name__ == "__main__": main()``````

#### Output

`````` Range [ 2 , 8 ] : Even [ 4 ], Odd [ 3 ]
Range [ 1 , 10 ] : Even [ 5 ], Odd [ 5 ]
Range [ 21 , 33 ] : Even [ 6 ], Odd [ 7 ]
Range [ 7 , 29 ] : Even [ 11 ], Odd [ 12 ]
Range [ 0 , 0 ] : Even [ 1 ], Odd [ 0 ]``````
``````#   Ruby program
#   Count Even and Odd numbers in a range

class EvenOddNumber
# Count the number of Even and Odd elements in given range
def count_even_odd(first, last)
if (first > last)
# Transform into a valid range
self.count_even_odd(last, first)
else
even = 0
odd = 0
# Count number of element
length = (last - first) + 1
if (first % 2 == 0)
if (last % 2 == 0)
# When both given number is even
even = (length / 2) + 1
else
even = length / 2
end

odd = length - even
else
if (last % 2 != 0)
odd = (length / 2) + 1
else
odd = length / 2
end

even = length - odd
end

print("\n Range [", first ,",", last ,"] : Even [", even ,"], Odd [", odd ,"] ")
end

end

end

def main()
obj = EvenOddNumber.new()
# Test case
obj.count_even_odd(2, 8)
obj.count_even_odd(1, 10)
obj.count_even_odd(21, 33)
obj.count_even_odd(7, 29)
obj.count_even_odd(0, 0)
end

main()``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd  ``````
``````/*
Scala program
Count Even and Odd numbers in a range
*/
class EvenOddNumber
{
//Count the number of Even and Odd elements in given range
def count_even_odd(first: Int, last: Int): Unit = {
if (first > last)
{
//Transform into a valid range
count_even_odd(last, first);
}
else
{
var even: Int = 0;
var odd: Int = 0;
//Count number of element
var length: Int = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = ((length / 2).toInt) + 1;
}
else
{
even = (length / 2).toInt;
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = ((length / 2).toInt) + 1;
}
else
{
odd = (length / 2).toInt;
}
even = length - odd;
}
print("\n Range [" + first + "," + last + "] : Even [" + even + "], Odd [" + odd + "] ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: EvenOddNumber = new EvenOddNumber();
//Test case
obj.count_even_odd(2, 8);
obj.count_even_odd(1, 10);
obj.count_even_odd(21, 33);
obj.count_even_odd(7, 29);
obj.count_even_odd(0, 0);
}
}``````

#### Output

`````` Range [2,8] : Even , Odd 
Range [1,10] : Even , Odd 
Range [21,33] : Even , Odd 
Range [7,29] : Even , Odd 
Range [0,0] : Even , Odd ``````
``````/*
Swift 4 program
Count Even and Odd numbers in a range
*/
class EvenOddNumber
{
//Count the number of Even and Odd elements in given range
func count_even_odd(_ first: Int, _ last: Int)
{
if (first > last)
{
//Transform into a valid range
self.count_even_odd(last, first);
}
else
{
var even: Int = 0;
var odd: Int = 0;
//Count number of element
let length: Int = (last - first) + 1;
if (first % 2 == 0)
{
if (last % 2 == 0)
{
//When both given number is even
even = (length / 2) + 1;
}
else
{
even = length / 2;
}
odd = length - even;
}
else
{
if (last % 2 != 0)
{
odd = (length / 2) + 1;
}
else
{
odd = length / 2;
}
even = length - odd;
}
print("\n Range [\(first,last)] : Even [", even ,"], Odd [", odd ,"]", terminator: "");
}
}
}
func main()
{
let obj: EvenOddNumber = EvenOddNumber();
//Test case
obj.count_even_odd(2, 8);
obj.count_even_odd(1, 10);
obj.count_even_odd(21, 33);
obj.count_even_odd(7, 29);
obj.count_even_odd(0, 0);
}
main();``````

#### Output

`````` Range [(2, 8)] : Even [ 4 ], Odd [ 3 ]
Range [(1, 10)] : Even [ 5 ], Odd [ 5 ]
Range [(21, 33)] : Even [ 6 ], Odd [ 7 ]
Range [(7, 29)] : Even [ 11 ], Odd [ 12 ]
Range [(0, 0)] : Even [ 1 ], Odd [ 0 ]``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.