Site icon Codeforcoding

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

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find whether a number is even or odd

 

C++ program to find a number is even or odd using the function

C++ program to separate Odd and Even numbers from an array

C++ program to display all even and odd numbers from 1 to n

C++ program calculate Average of odd and even numbers in an array 

C++ program to calculate the sum of odd and even numbers

C++ program to find whether a number is even or odd

 

Python program check whether a number is odd or even

Python program to check a number is even or odd using the function

Python program to display even and odd numbers without if

Python program to display even and odd number in the given range

Separate odd and even numbers in a list to different two list

Python Program to find odd and even numbers from a list

 

 

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