Count Even and Odd numbers in a range

Here given code implementation process.

// 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 ]


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.

New Comment







© 2021, kalkicode.com, All rights reserved