Python code to display even and odd number from 1 to n
- Home
- Find elements
- Python code to display even and odd number from 1 to n
- On
- By
- 0 Comment
- Categories: Find elements
Python code to display even and odd number from 1 to n
Python code to display even and odd number from 1 to n
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
Python code to print Even and odd number from 1 to n Using for loop
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
Python Code to print Even and odd number from 1 to n Using while loop
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
Python program to print Even and odd number from 1 to n Without if statements
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