Friday

Python Program — Display Even and Odd Numbers


Using Python to loop through a specified range of numbers to display odd and even numbers.

Even numbers are numbers divisible by 2, while odd numbers are numbers whose remainder equals 1 if divided by two.

This program displays even and odd numbers between 1 and a specified limit. The limit can be changed by changing the value of the last number variable. The initial even or initial odd number value can also be changed to specify the start number.

last_number = 30 #the end number - value can be changed
# Display Even Number
print("Even Numbers:")
i = 2 #initial even number
while i <= last_number:
  print(i),
  i += 2 #increment i by 2
#Display Odd Numbers
print("\n\nOdd Numbers:")
i = 1 #initial odd number
while i <= last_number:
print(i),
i += 2 #increment i by 2

Download program here.

No comments:

Post a Comment