In this Python program, we will learn how to print the odd numbers in a given list.
Here is the source code of the program to print the odd numbers in a given list.
# Python Program to Print the Odd Numbers in a Given List Using For Loop
NumList = []
# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List
print("\nOriginal List: ",NumList)
print("\nOdd Numbers in the Given List are: ")
for j in range(Number):
if(NumList[j] % 2 != 0):
print(NumList[j], end = ' ')
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 78
Enter the Value of 2 Element: 45
Enter the Value of 3 Element: 123
Enter the Value of 4 Element: 65
Enter the Value of 5 Element: 85
Enter the Value of 6 Element: 22
Original List: [78, 45, 123, 65, 85, 22]
Odd Numbers in the Given List are:
45 123 65 85
Program 2: Python Program to Print the Odd Numbers in a Given List Using While Loop
# Python Program to Print the Odd Numbers in a Given List Using While Loop
NumList = []
j = 0
# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List
print("\nOriginal List: ",NumList)
print("\nOdd Numbers in the Given List are: ")
while(j < Number):
if(NumList[j] % 2 != 0):
print(NumList[j], end = ' ')
j = j + 1
Enter the Total Number of List Elements: 7
Enter the Value of 1 Element: 45
Enter the Value of 2 Element: 12
Enter the Value of 3 Element: 63
Enter the Value of 4 Element: 56
Enter the Value of 5 Element: 85
Enter the Value of 6 Element: 456
Enter the Value of 7 Element: 78
Original List: [45, 12, 63, 56, 85, 456, 78]
Odd Numbers in the Given List are:
45 63 85
Comments