Count Odd and Even numbers in a range from L to R - GeeksforGeeks

30 Apr, 2021
Given two numbers L and R, the task is to count the number of odd 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, 6Input: l = 4, r = 8 Output: 2  
 
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 odd number will be N/2 + 1 and even numbers = N – countofOdd.Else, count of odd numbers will be N/2 and even numbers = N – countofOdd.
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 odd number will be N/2 + 1 and even numbers = N – countofOdd.Else, count of odd numbers will be N/2 and even numbers = N – countofOdd.
If L or R is odd, then the count of odd number will be N/2 + 1 and even numbers = N – countofOdd.
Else, count of odd numbers will be N/2 and even numbers = N – countofOdd.
Below is the implementation of the above approach: 
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h> using namespace std; // Return the number of odd numbers// in the range [L, R]int countOdd(int L, int R){     int N = (R - L) / 2;     // if either R or L is odd    if (R % 2 != 0 || L % 2 != 0)        N += 1;     return N;} // Driver codeint 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;} // This code is contributed by Rituraj Jain
// Java implementation of the above approach class GFG {     // Return the number of odd numbers    // in the range [L, R]    static int countOdd(int L, int R)    {        int N = (R - L) / 2;         // if either R or L is odd        if (R % 2 != 0 || L % 2 != 0)            N++;         return N;    }     // Driver code    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 implementation of the# above approach # Return the number of odd numbers# in the range [L, R]def countOdd(L, R):     N = (R - L) // 2     # if either R or L is odd    if (R % 2 != 0 or L % 2 != 0):        N += 1     return N # Driver codeif __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) # This code is contributed by ita_c
// C# implementation of the above approachusing System; class GFG{     // Return the number of odd numbers    // in the range [L, R]    static int countOdd(int L, int R)    {        int N = (R - L) / 2;         // if either R or L is odd        if (R % 2 != 0 || L % 2 != 0)            N++;         return N;    }     // Driver code    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);    }} // This code is contributed by Ryuga
<?php// PHP implementation of the above approach // Return the number of odd numbers// in the range [L, R]function countOdd($L, $R){    $N = ($R - $L) / 2;     // if either R or L is odd    if ($R % 2 != 0 || $L % 2 != 0)        $N++;     return $N;} // Driver code$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; // This code is contributed// by Akanksha Rai?>
<script> // Javascript implementation// of the above approach  // Return the number of odd numbers// in the range [L, R]function countOdd( L, R){     let N = Math.floor((R - L) / 2);     // if either R or L is odd    if (R % 2 != 0 || L % 2 != 0)        N += 1;     return N;}      // Driver Code         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>
Count of odd numbers is 3
Count of even numbers is 2
 
ankthon
ukasp
Akanksha_Rai
rituraj_jain
jana_sayantan
number-theory
Mathematical
School Programming
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Program for factorial of a number
Operators in C / C++
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects