Count Odd and Even numbers in a range from L to R | C ++ implementation of the above approach ; Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }