Write a Python Program to Print Even Numbers from 1 to N using While Loop and For Loop with an example.
Python Program to Print Even Numbers from 1 to N using For Loop
This Python program allows the user to enter the limit value. Next, Python is going to print even numbers from 1 to that user entered limit value.
In this example, Python For Loop makes sure that the number is between 1 and maximum limit value.
TIP: I suggest you refer to Python Even or Odd Program article to understand the Python logic behind Even numbers.
# Python Program to Print Even Numbers from 1 to N maximum = int(input(" Please Enter the Maximum Value : ")) for number in range(1, maximum+1): if(number % 2 == 0): print("{0}".format(number))
Python Printing Even numbers output
Please Enter the Maximum Value : 10
2
4
6
8
10
Python Program to find Even Numbers from 1 to 100 without If Statement
This Python even numbers from 1 to 100 examples is the same as above. But, we altered the Python For Loop to eliminate If block.
If you observe the below Python program, we started the range from 2, and we used the counter value is 2. It means, for the first iteration number is 2, second iteration number = 4 (not 3) so on.
# Python Program to Print Even Numbers from 1 to N maximum = int(input(" Please Enter the Maximum Value : ")) for number in range(2, maximum+1, 2): print("{0}".format(number))
Python Even numbers output
Please Enter the Maximum Value : 20
2
4
6
8
10
12
14
16
18
20
Python Program to display Even Numbers using While Loop
In this Python even numbers program, we just replaced the For Loop with While Loop.
# Python Program to Print Even Numbers from 1 to N maximum = int(input(" Please Enter the Maximum Value : ")) number = 1 while number <= maximum: if(number % 2 == 0): print("{0}".format(number)) number = number + 1
Python Printing Even numbers output
Please Enter the Maximum Value : 14
2
4
6
8
10
12
14
Python Program to display Even Numbers from 1 to 100
This example allows the user to enter Minimum and maximum value — next, Python prints even numbers between Minimum and maximum value.
# Python Program to Print Even Numbers from Min to Max minimum = int(input(" Please Enter the Minimum Value : ")) maximum = int(input(" Please Enter the Maximum Value : ")) for number in range(minimum, maximum+1): if(number % 2 == 0): print("{0}".format(number))