easy
0 views
Odd Integers In Range Program in Python
Print all odd integers between two given integers, excluding the boundaries.
Understand the Problem
Problem Statement
Write a program that accepts two integers X and Y, then prints all odd integers between them (excluding X and Y themselves).
Constraints
- -999999 <= X <= 9999999
- X < Y <= 9999999
- Output must contain only odd integers
- Output must be space-separated
Examples
Example 1
Input
1
11Output
3 5 7 9Explanation
Between 1 and 11, the integers are 2,3,4,5,6,7,8,9,10. Among these, the odd integers are 3,5,7,9.
Example 2
Input
24
30Output
25 27 29Explanation
Between 24 and 30, the integers are 25,26,27,28,29. Among these, the odd integers are 25,27,29.
Solution
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
int first = 1;
for (int i = x + 1; i < y; i++) {
if (i % 2 != 0) {
if (!first) {
printf(" ");
}
printf("%d", i);
first = 0;
}
}
printf("\n");
return 0;
}Time:O(Y-X)
Space:O(1)
Approach:
C Implementation:
1. Read integers X and Y using scanf()
2. Use a loop from (X+1) to (Y-1) to iterate through the range
3. Check if each number is odd using (i % 2 != 0)
4. Print the number if odd, with proper spacing using a 'first' flag to avoid trailing spaces
5. Print newline at the end
Visual Explanation
Loading diagram...