Posted on by Kalkicode
Code Mathematics

Count Even and Odd numbers in a range

The problem at hand involves counting the number of even and odd integers within a given range. For instance, in the range [2, 8], there are 4 even numbers (2, 4, 6, and 8) and 3 odd numbers (3, 5, and 7). This task can be approached programmatically, and we'll discuss a solution in detail, along with code and explanations.

Problem Statement

The task is to develop a program that takes two integers, 'first' and 'last', representing the range [first, last] (inclusive), and then calculates the count of even and odd numbers within this range. The program will output these counts for each range tested.

Example

For the input range [2, 8], there are 4 even numbers (2, 4, 6, and 8) and 3 odd numbers (3, 5, and 7). So, the output should be:

``Range [2,8] : Even [4], Odd [3]``

Idea to Solve

To solve this problem, we can follow these steps:

1. Check if 'first' is greater than 'last'. If it is, swap the values to ensure 'first' is smaller than 'last'.
2. Calculate the length of the range using the formula: `length = (last - first) + 1`.
3. Determine whether 'first' is even or odd.
4. Based on the parity of 'first', calculate the counts of even and odd numbers in the range.
5. Print the results.

Pseudocode

``````count_even_odd(first, last)
if first > last
swap(first, last)

length = (last - first) + 1

if first is even
if last is even
even = (length / 2) + 1
else
even = length / 2
odd = length - even
else
if last is odd
odd = (length / 2) + 1
else
odd = length / 2
even = length - odd

print "Range [", first, ",", last, "] : Even [", even, "], Odd [", odd, "]"``````

Algorithm Explanation

1. The `count_even_odd` function takes 'first' and 'last' as input arguments.
2. If 'first' is greater than 'last', we swap their values to ensure 'first' is smaller than 'last'.
3. We calculate the length of the range as 'length = (last - first) + 1'.
4. If 'first' is even, we check whether 'last' is even. If both are even, we calculate the count of even numbers as `(length / 2) + 1`, otherwise as `length / 2`. The count of odd numbers is then calculated as `length - even`.
5. If 'first' is odd, we check whether 'last' is odd. If both are odd, we calculate the count of odd numbers as `(length / 2) + 1`, otherwise as `length / 2`. The count of even numbers is then calculated as `length - odd`.
6. Finally, we print the range, counts of even and odd numbers.

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 [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]``````
``````/*
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 [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]``````
``````//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 [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]``````
``````//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 [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]``````
``````<?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 [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]``````
``````/*
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 [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]``````
``````#   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 [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] ``````
``````/*
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 [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]``````
``````/*
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 ]``````

Resultant Output Explanation

Let's analyze the provided test cases and their outputs:

1. `count_even_odd(2, 8);` results in `Range [2,8] : Even [4], Odd [3]`.

• Even numbers: 2, 4, 6, 8
• Odd numbers: 3, 5, 7
2. `count_even_odd(1, 10);` results in `Range [1,10] : Even [5], Odd [5]`.

• Even numbers: 2, 4, 6, 8, 10
• Odd numbers: 1, 3, 5, 7, 9
3. `count_even_odd(21, 33);` results in `Range [21,33] : Even [6], Odd [7]`.

• Even numbers: 22, 24, 26, 28, 30, 32
• Odd numbers: 21, 23, 25, 27, 29, 31, 33
4. `count_even_odd(7, 29);` results in `Range [7,29] : Even [11], Odd [12]`.

• Even numbers: 8, 10, 12, ..., 28
• Odd numbers: 7, 9, 11, ..., 29
5. `count_even_odd(0, 0);` results in `Range [0,0] : Even [1], Odd [0]`.

• Even number: 0

Time Complexity

The time complexity of this algorithm is constant, O(1), as the number of operations performed is not dependent on the size of the input range. The calculations involved are straightforward and involve a constant number of arithmetic operations.

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.

Categories
Relative Post