Program to print positive numbers in the given range using Python

by | Apr 4, 2021 | Python Programs

Home » Python » Python Programs » Program to print positive numbers in the given range using Python

Introduction

In previous section we have seen the condition for positive number. Now we will print the positive numbers between the range give.

Program to print positive numbers in the given range

Program

positive_list = []
n = int(input("Enter the lower bound of the list "))
m = int(input("Enter the upper bound of the list "))
# Check range
if n & m <0:
    print("There is no positive number in the given range")
else:
# Using for loop
    for i in range(n,m+1):
        if i >= 0:
            positive_list.append(i)
    print("Positive numbers in the given range {0} and {1} using for loop are {2}".format(n, m, positive_list))

Output

Program to print positive numbers in the given range Output

Explanation

In the above python code we have taken the user input range from the user and checked whether the range given are negative or not. If any of the value range is positive integer, the for loop will iterate over the range and append the values greater than 0 to the list. When all the values are traversed the list is displayed on the screen.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author