start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
print("Odd numbers between", start, "and", end, "are:")
for num in range(start, end + 1):
if num % 2 != 0:
print(num, end=" ")
Code Explanation:
1. Prompt User for Input:
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
The program prompts the user to input two integers:
start: The beginning of the range.
end: The end of the range.
int(input(...)) ensures the input is converted from a string to an integer.
2. Print a Header:
print("Odd numbers between", start, "and", end, "are:")
This line prints a message specifying the range of numbers being analyzed.
3. Loop Through the Range:
for num in range(start, end + 1):
range(start, end + 1) generates all integers from start to end (inclusive).
The loop iterates through each number in this range, assigning it to the variable num.
4. Check for Odd Numbers:
if num % 2 != 0:
This checks whether num is odd:
num % 2: Computes the remainder when num is divided by 2.
!= 0: Ensures the remainder is not zero (odd numbers always leave a remainder of 1).
5. Print Odd Numbers:
print(num, end=" ")
If num is odd, it is printed on the same line.
end=" " ensures the numbers are printed with a space in between, instead of a new line after each.
#source code --> clcoding.com
0 Comments:
Post a Comment