Write a program to take two numbers from the user and print ODD numbers between the two numbers (both inclusive).
Answers
int main()
{
int count, limit;
printf("Enter start and end value to generate Odd numbers\n");
scanf("%d%d", &count, &limit);
printf("\nOdd numbers between %d and %d are:\n", count, limit);
while(count <= limit)
{
if(count % 2 != 0)
{
printf("%d\n", count);
}
count++;
}
return 0;
}
Explanation:
There is no need to go through the range. Take a look at the first number: if it is odd, build a new range with the step of 2 that starts at that number. If it is even, then start at the next number.
def oddNumbers(l, r):
if l % 2 == 1:
return list(range(l, r + 1, 2))
else:
return list(range(l + 1, r + 1, 2))
Or, in a more Pythonic way:
def oddNumbers(l, r):
return list(range(l if l