
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Find the number of prime numbers within a given range of numbers
When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The ‘%’ modulus operator is used to find the prime numbers.
Example
Below is a demonstration of the same
lower_range = 670 upper_range = 699 print("The lower and upper range are :") print(lower_range, upper_range) print("The prime numbers between", lower_range, "and", upper_range, "are:") for num in range(lower_range, upper_range + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)
Output
The lower and upper range are : 670 699 The prime numbers between 670 and 699 are: 673 677 683 691
Explanation
The upper range and lower range values are entered and displayed on the console.
The numbers are iterated over.
It is checked to see if they are greater than 1 since 1 is neither a prime number nor a composite number.
The numbers are iterated, and ‘%’ with 2.
This way the prime number is found, and displayed on console.
Else it breaks out of the loop.
- Related Questions & Answers
- Prime numbers within a range in JavaScript
- How to find Kaprekar numbers within a given range using Python?
- PHP program to find the sum of odd numbers within a given range
- Sum of prime numbers between a range - JavaScript
- Program to find out the number of special numbers in a given range in Python
- Write a Golang program to find prime numbers in a given range
- Program to find bitwise AND of range of numbers in given range in Python
- Finding the count of numbers divisible by a number within a range using JavaScript
- Prime numbers in a range - JavaScript
- Find a range of composite numbers of given length in C++
- Golang Program to Print Odd Numbers Within a Given Range
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Print prime numbers in a given range using C++ STL
- Python Generate random numbers within a given range and store in a list
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
Advertisements