Count Odd and Even numbers in a range from L to R
Last Updated :
18 Mar, 2023
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
Brute Force Approach:
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
# Python equivalent of the code
l, h = 3, 7 # initializing l and h
odd, even = 0, 0 # initializing odd and even
# for loop to check for odd and even numbers
for i in range(l, h+1):
if i % 2: # checking for odd numbers
odd += 1 # increase odd count
else:
even += 1 # increase even count
# printing the output
print("Odd count is :", odd)
print("Even count is :", even)
Java
// Java equivalent of the code
import java.util.Scanner;
public class GFG {
public static void main(String[] args)
{
// initializing l and h
int l = 3, h = 7;
// initializing odd and even
int odd = 0, even = 0;
// for loop to check for odd and even numbers
for (int i = l; i <= h; i++) {
// checking for odd numbers
if (i % 2 == 1)
odd++; // increase odd count
else
even++; // increase even count
}
// printing the output
System.out.println("Odd count is : " + odd);
System.out.println("Even count is : " + even);
}
}
C#
// C# equivalent of the code
using System;
class GFG {
public static void Main(string[] args)
{
// initializing l and h
int l = 3, h = 7;
// initializing odd and even
int odd = 0, even = 0;
// for loop to check for odd and even numbers
for (int i = l; i <= h; i++) {
// checking for odd numbers
if (i % 2 == 1)
odd++; // increase odd count
else
even++; // increase even count
}
// printing the output
Console.WriteLine("Odd count is : " + odd);
Console.WriteLine("Even count is : " + even);
}
}
JavaScript
// Javascript equivalent of the code
// Define the lower and higher limits of the range
let l = 3;
let h = 7;
// Initialize the counters for odd and even numbers
let odd = 0;
let even = 0;
// Loop through the range from l to h (inclusive)
for(let i = l; i <= h; i++) {
if(i % 2) { // Check if the number is odd
odd++; // Increment the odd counter if the number is odd
} else {
even++; // Increment the even counter if the number is even
}
}
// Print the counts of odd and even numbers
console.log("Odd count is: " + odd);
console.log("Even count is: " + even);
OutputOdd count is : 3
Even count is : 2
Complexity Analysis:
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++
// 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 code
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;
}
// This code is contributed by Rituraj Jain
Java
// 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
# 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 code
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)
# This code is contributed by ita_c
C#
// C# implementation of the above approach
using 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
// 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 intval($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
?>
JavaScript
<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>
OutputCount 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.
Similar Reads
Sum of all even numbers in range L and R Given two integers L and R, the task is to find the sum of all even numbers in range L and R. Examples: Input: L = 2, R = 5 Output: 6 2 + 4 = 6 Input: L = 3, R = 8 Output: 18 Method-1: Iterate from L to R and sum all the even numbers in that range. Method-2: Find the sum all the natural numbers from
5 min read
XOR of all even numbers from a given range Given two integers L and R, the task is to calculate Bitwise XOR of all even numbers in the range [L, R]. Examples: Example: Input: L = 10, R = 20 Output: 30 Explanation: Bitwise XOR = 10 ^ 12 ^ 14 ^ 16 ^ 18 ^ 20 = 30 Therefore, the required output is 30. Example: Input: L = 15, R = 23 Output: 0 Exp
6 min read
C++ program to print all Even and Odd numbers from 1 to N Given a number N, the task is to print N even numbers and N odd numbers from 1. Examples: Input: N = 5 Output: Even: 2 4 6 8 10 Odd: 1 3 5 7 9 Input: N = 3 Output: Even: 2 4 6 Odd: 1 3 5 Approach: For Even numbers:Even numbers are numbers that are divisible by 2.To print even numbers from 1 to N, tr
4 min read
Count even and odd Bitwise XORs of consecutive numbers in a range [L, R] starting from L Given two integers L and R, the task is to find the count of even and odd Bitwise XOR values of consecutive numbers from the range [L, R] starting from L. Examples: Input: L = 2, R = 7Output: Even = 3, Odd = 3Explanation: Taking bitwise XOR of continuous numbers:22 ^ 3 = 12 ^ 3 ^ 4 = 52 ^ 3 ^ 4 ^ 5
7 min read
Count total set bits in all numbers from range L to R Given two positive integers L and R, the task is to count the total number of set bits in the binary representation of all the numbers from L to R. Examples: Input: L = 3, R = 5 Output: 5 Explanation: (3)10 = (11)2, (4)10 = (100)2, (5)10 = (101)2 So, Total set bit in range 3 to 5 is 5 Input: L = 10,
15+ min read
Count odd and even digits in a number in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.Given a number and task is to find the number of odd and
2 min read
Count of numbers having only one unset bit in a range [L,R] Given two integers L and R, the task is to count the numbers having only one unset bit in the range [L, R]. Examples: Input: L = 4, R = 9Output: 2Explanation:The binary representation of all numbers in the range [4, 9] are 4 = (100)2 5 = (101)2 6 = (110)2 7 = (111)2 8 = (1000)2 9 = (1001)2 Out of al
11 min read
Sum of all even factors of numbers in the range [l, r] Given a range [l, r], the task is to find the sum of all the even factors of the numbers from the given range.Examples: Input: l = 6, r = 8 Output: 22 factors(6) = 1, 2, 3, 6, evenfactors(6) = 2, 6 sumEvenFactors(6) = 2 + 6 = 8 factors(7) = 1, 7, No even factors factors(8) = 1, 2, 4, 8, evenfactors(
15+ min read
Count of numbers in given range L to R which are present in a Matrix Given a matrix(mat[][]), which is sorted row and column-wise in increasing order. Two integers are given, L and R, our task is to count number of elements of the matrix within the range [L, R].Examples: Input: L = 3, R = 11, matrix = {{1, 6, 9} {2, 7, 11} {3, 8, 12}} Output: 6 Explanation: The eleme
8 min read
Count set bits in the Kth number after segregating even and odd from N natural numbers Given two integers N and K, the task is to find the count of set bits in the Kth number in the Odd-Even sequence made of the number from the range [1, N]. The Odd-Even sequence first contains all the odd numbers from 1 to N and then all the even numbers from 1 to N.Examples: Input: N = 8, K = 4 Outp
6 min read