Program to find the prime numbers between the given range using Python

by | Feb 1, 2021 | Python Programs

Home » Python » Python Programs » Program to find the prime numbers between the given range using Python

Introduction

In this section, of python programing we will learn the python code to find the prime numbers between the given range. A prime number is a natural number which is greater than one and is divisible only by 1 and the number itself. Example of prime numbers : 2, 3, 5, 7, 11, 13, 17,…….

Program

n = int(input("Please enter the start number: "))
m = int(input("Please enter the end number: "))
count = 0
for i in range(n,m):
    if i > 1:
        for j in range(2,i):
            if(i % j == 0):
                break
        else:
# Display prime number
            print(i)
            count = count + 1
print("Total prime numbers between {0} and {1} is {2}".format(n,m,count))

Output

Program to find the prime numbers between the given range using Python

Explanation

In the above python program we have created variable n and m which takes the integer input from the user and the variable count will keep the record of total prime numbers between the range and is initialised to zero. The for loop is used to check whether the numbers between the given range is prime or not. If the number is only divisible by itself it is called prime number and count will increase by 1. The output displayed is the prime numbers and total count of prime numbers.

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