Given two numbers L and R, the task is to count the number of odd and even numbers in the range L to R.
Examples:
Input: l = 3, r = 7
Output: 3 2
Count of odd numbers is 3 i.e. 3, 5, 7
Count of even numbers is 2 i.e. 4, 6
Input: l = 4, r = 8
Output: 2 3
Count of odd numbers is 2 i.e. 5, 7
Count of even numbers is 3 i.e. 4, 6, 8
we will just traverse over the range and output the count at last.
C++
#include <iostream>
using namespace std;
int main() {
int l=3,h=7;
int odd=0,even=0;
for ( int i=l;i<=h;i++){
if (i%2)++odd;
else ++even;
}
cout<< "Odd count is : " <<odd<<endl;
cout<< "Even count is : " <<even<<endl;
return 0;
}
|
Python3
l, h = 3 , 7
odd, even = 0 , 0
for i in range (l, h + 1 ):
if i % 2 :
odd + = 1
else :
even + = 1
print ( "Odd count is :" , odd)
print ( "Even count is :" , even)
|
Java
import java.util.Scanner;
public class GFG {
public static void main(String[] args)
{
int l = 3 , h = 7 ;
int odd = 0 , even = 0 ;
for ( int i = l; i <= h; i++) {
if (i % 2 == 1 )
odd++;
else
even++;
}
System.out.println( "Odd count is : " + odd);
System.out.println( "Even count is : " + even);
}
}
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
int l = 3, h = 7;
int odd = 0, even = 0;
for ( int i = l; i <= h; i++) {
if (i % 2 == 1)
odd++;
else
even++;
}
Console.WriteLine( "Odd count is : " + odd);
Console.WriteLine( "Even count is : " + even);
}
}
|
Javascript
let l = 3;
let h = 7;
let odd = 0;
let even = 0;
for (let i = l; i <= h; i++) {
if (i % 2) {
odd++;
} else {
even++;
}
}
console.log( "Odd count is: " + odd);
console.log( "Even count is: " + even);
|
Output
Odd count is : 3
Even count is : 2
Time Complexity: O(n).
Auxiliary Space: O(1).
Approach: Total numbers in the range will be (R – L + 1) i.e. N.
- If N is even then the count of both odd and even numbers will be N/2.
- If N is odd,
- If L or R is odd, then the count of the odd numbers will be N/2 + 1, and even numbers = N – countofOdd.
- Else, the count of odd numbers will be N/2 and even numbers = N – countofOdd.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOdd( int L, int R){
int N = (R - L) / 2;
if (R % 2 != 0 || L % 2 != 0)
N += 1;
return N;
}
int main()
{
int L = 3, R = 7;
int odds = countOdd(L, R);
int evens = (R - L + 1) - odds;
cout << "Count of odd numbers is " << odds << endl;
cout << "Count of even numbers is " << evens << endl;
return 0;
}
|
Java
class GFG {
static int countOdd( int L, int R)
{
int N = (R - L) / 2 ;
if (R % 2 != 0 || L % 2 != 0 )
N++;
return N;
}
public static void main(String[] args)
{
int L = 3 , R = 7 ;
int odds = countOdd(L, R);
int evens = (R - L + 1 ) - odds;
System.out.println( "Count of odd numbers is " + odds);
System.out.println( "Count of even numbers is " + evens);
}
}
|
Python 3
def countOdd(L, R):
N = (R - L) / / 2
if (R % 2 ! = 0 or L % 2 ! = 0 ):
N + = 1
return N
if __name__ = = "__main__" :
L = 3
R = 7
odds = countOdd(L, R)
evens = (R - L + 1 ) - odds
print ( "Count of odd numbers is" , odds)
print ( "Count of even numbers is" , evens)
|
C#
using System;
class GFG
{
static int countOdd( int L, int R)
{
int N = (R - L) / 2;
if (R % 2 != 0 || L % 2 != 0)
N++;
return N;
}
public static void Main()
{
int L = 3, R = 7;
int odds = countOdd(L, R);
int evens = (R - L + 1) - odds;
Console.WriteLine( "Count of odd numbers is " + odds);
Console.WriteLine( "Count of even numbers is " + evens);
}
}
|
PHP
<?php
function countOdd( $L , $R )
{
$N = ( $R - $L ) / 2;
if ( $R % 2 != 0 || $L % 2 != 0)
$N ++;
return intval ( $N );
}
$L = 3; $R = 7;
$odds = countOdd( $L , $R );
$evens = ( $R - $L + 1) - $odds ;
echo "Count of odd numbers is " . $odds . "\n" ;
echo "Count of even numbers is " . $evens ;
?>
|
Javascript
<script>
function countOdd( L, R){
let N = Math.floor((R - L) / 2);
if (R % 2 != 0 || L % 2 != 0)
N += 1;
return N;
}
let L = 3, R = 7;
let odds = countOdd(L, R);
let evens = (R - L + 1) - odds;
document.write(
"Count of odd numbers is " + odds + "</br>"
);
document.write(
"Count of even numbers is " + evens + "</br>"
);
</script>
|
Output
Count of odd numbers is 3
Count of even numbers is 2
Time Complexity: O(1), since there is only a basic arithmetic operation that takes constant time.
Auxiliary Space: O(1), since no extra space has been taken.
Contact Us