In this tutorial, we discuss Python code to display even and number from 1 to n.
Here, we show you, How to write a Python program to print Even and odd numbers using for loop and while loop.
What is Even or Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
then, when it is divided by 2 and balance becomes 1 they are called odd numbers.
In my previous post, I have explained the various ways to check whether the number is even or odd in Python language
This Python program allows to enter a value to print odd and even numbers from 1 to targeted number using for loop.
Program 1
#Python program to print even numner from 1 to n max=int(input("Please Enter the Maximum value: ")) for num in range(1, max+1): if(num % 2 == 0): print("{0}".format(num)) #Python program to print odd numner from 1 to n max=int(input("Please Enter the Maximum value: ")) for num in range(1, max+1): if(num % 2 != 0): print("{0}".format(num))
When the above code is executed, it produces the following results
Please Enter the Maximum value: 20 2 4 6 8 10 12 14 16 18 20 Please Enter the Maximum value: 20 1 3 5 7 9 11 13 15 17 19
Program 2
This Python program allows to enter a value to print odd and even numbers from 1 to targeted number using while loop.
#Python program to print even numner from 1 to n max=int(input("Please Enter the Maximum value: ")) num=1 while num <=max: if(num % 2 == 0): print("{0}".format(num)) num=num+1 #Python program to print odd numner from 1 to n max=int(input("Please Enter the Maximum value: ")) num=1 while num <=max: if(num % 2 != 0): print("{0}".format(num)) num=num+1
When the above code is executed, it produces the following results
Please Enter the Maximum value: 20 2 4 6 8 10 12 14 16 18 20 Please Enter the Maximum value: 20 1 3 5 7 9 11 13 15 17 19
Program 3
This Python program allows to enter a value to print odd and even numbers from 1 to targeted number without if statements
#Python program to print Even numbers from 1 to n max=int(input("Please Enter the maximum value: ")) for num in range(2, max+1, 2): print("{0}".format(num)) #Python program to print odd numbers from 1 to n max=int(input("Please Enter the maximum value: ")) for num in range(1, max+1, 2): print("{0}".format(num))
When the above code is executed, it produces the following results
Please Enter the maximum value: 20 2 4 6 8 10 12 14 16 18 20 Please Enter the maximum value: 20 1 3 5 7 9 11 13 15 17 19
Suggested for you
Similar post
Python program to check whether a number odd or even
Python program to check a number odd or even using function
Python program to display even and odd number in the given range
Python code to display all even and odd numbers without if
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…