Site icon Codeforcoding

Python program to display all odd or even numbers 1 to n with label

Python program to display all odd or even numbers 1 to n with label

In this article, we will discuss the concept of the Python program to display all  odd or even numbers with label using loops

In this post, we will learn how to find and display odd and even numbers with label using the loops.

Here, we will use for loop, while loop to find and display odd and even numbers from 1 to n in Python programming language.

 

Python program to display all odd or even numbers

Using for loop – program 1

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label from 1 to entered number using for loop.

#Python program to print odd and even number from 1 to n
max=int(input("Please enter the maximum number: "));
#Takes input from the user for variable max
for number in range(1,max+2):
    if(number%2 !=0):
        print(number,"is odd")
    else:
        print(number,"is even",)

When the above code is executed, it produces the following results

Please enter the maximum number: 20
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
20 is even

 

Using for loop – program 2

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label 1 to entered number using for loop.

#Python program to print odd and even number from 1 to n
max=int(input("Please enter any maximum value"))
#Takes input from the user for variable max
for number in range(1, max+1):
    if(number%2!=0):
        print("{0}".format(number),"is odd")
    else:
        print("{0}".format(number),"is even")
        

When the above code is executed, it produces the following results

Please enter the maximum number: 20
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
20 is even

Using while loop – program 1

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label from 1 to entered number using while loop.

#Python program to print odd and even number from 1 to n
max=int(input("Please enter any maximum value"))
num=1
while num<=max:
    if(num%2!=0):
        print(num,"is odd")
    else:
        print(num,"is even")
    num=num+1
        

When the above code is executed, it produces the following results

Please enter the maximum number: 20
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
20 is even

 

 

Using while loop – program 2

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label 1 to entered number using while loop.

#Python program to print odd and even number from 1 to n
max=int(input("Please enter any maximum value"))
num=1
while num<=max:
    if(num%2!=0):
        print("{0}".format(num),"is odd")
    else:
        print("{0}".format(num),"is even")
    num=num+1

 

When the above code is executed, it produces the following results

Please enter the maximum number: 20
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
20 is even

 

 

 

Similar post

C program to find the number is odd or even with label using loops

Java program to find the number is odd or even with label using loops

C++ program to find the number is odd or even with label using loops

C++ program to find odd or even number using switch statements

C  program to find odd or even number using switch statements

Java program to check odd and even using recursion

C program to check odd and even using recursion

C++ program to check odd and even using recursion

Python program to check odd and even using recursion

Python program to check odd and even using function

Java program to check odd and even using Method

C program to check odd and even using function

 

 

Suggested for you

Python language for loop

Python language while loop

Python language data type

Python language operator

Java program to display all odd or even numbers 1 to n with label
Java program to find odd or even number using switch statements
Exit mobile version