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

For loop in Python

While loop in Python

If statements in Python

 

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

 

Calculate power of a number using recursion in C language
C program to display even and odd number in given range
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago