
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count odd numbers in an interval range using Python
Suppose we have two non-negative numbers left and right. We have to find the number of odd numbers between left and right (inclusive).
So, if the input is like left = 3, right = 15, then the output will be 7 because there are 7 odd numbers in range, these are [3,5,7,9,11,13,15], there are 7 elements.
To solve this, we will follow these steps −
if left is odd or right is odd, then
return 1 + quotient of (right-left) / 2
otherwise,
return quotient of (right-left) / 2
Example (Python)
Let us see the following implementation to get better understanding −
def solve(left, right): if left % 2 == 1 or right % 2 == 1: return (right-left) // 2 + 1 else: return (right-left) // 2 left = 3 right = 15 print(solve(left, right))
Input
3, 15
Output
7
- Related Articles
- Python program to print all odd numbers in a range
- Python Program to Print Numbers in an Interval
- Python program to Count Even and Odd numbers in a List
- Program to find count of numbers having odd number of divisors in given range in C++
- Python program to print all Prime numbers in an Interval
- How to Print all Prime Numbers in an Interval using Python?
- Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
- Count Odd and Even numbers in a range from L to R in C++
- Golang Program to Print Odd Numbers Within a Given Range
- 8085 program to count total odd numbers in series of 10 numbers
- C++ Program to count ordinary numbers in range 1 to n
- C++ program to find numbers with K odd divisors in a given range
- Python program to count unset bits in a range.
- Python program to print odd numbers in a list
- PHP program to find the sum of odd numbers within a given range

Advertisements