print the numbers from 1 to 100, but only print the even numbers. use a while loop in python

main.py
# initialize the counter to 1
i = 1

# while loop to print even numbers from i=1 to i=100
while i <= 100:
  if i % 2 == 0:                   # check if i is divisible by 2 (i.e. even number)
    print(i)                       # print i if it is an even number
  i += 1                           # increments i by 1 for the next iteration
338 chars
9 lines

In this code, we use a while loop to print even numbers from 1 to 100. Inside the loop, we use a conditional statement to check if the current value of i is an even number. If the number is even, we print it to the console. We then increment the value of the counter i by 1 and repeat the process until we reach the number 100.

gistlibby LogSnag