Prime Numbers in Python: Program to Print in a Range
Python Program to Print Prime Numbers Within a Given Range
Prime numbers are fundamental in many areas of mathematics and computer science. In programming, they are frequently used for cryptography, algorithms, and solving various real-world problems. In this article, we will show you how to write a Python program to print all the prime numbers within a given range.Whether you’re a beginner or a seasoned coder, this simple task can enhance your understanding of loops, conditional statements, and prime number algorithms.
What Are Prime Numbers?
A prime number is a natural number greater than 1 that has no divisors other than 1 and itself. In other words, a prime number is a number that cannot be formed by multiplying two smaller natural numbers.
Examples of Prime Numbers:
2, 3, 5, 7, 11, 13, 17, and so on.
Non-Prime Numbers (or Composite Numbers):
4, 6, 8, 9, 10, 12, etc.
Problem Overview: Printing Prime Numbers Within a Range
In this article, you’ll learn how to find and print all the prime numbers that lie between two user-defined numbers, the lower range and the upper range.
Sample Input:
go
Enter lower range: 3
Enter upper range: 15
Sample Output:
3 5 7 11 13
Algorithm to Print Prime Numbers in Python
To achieve the task of printing prime numbers within a range, follow these steps:
Step 1: Input the Range
First, take two integer inputs from the user: the lower and upper bounds of the range.
Step 2: Loop Through the Range
Using a for loop, iterate through all the numbers from the lower range to the upper range.
Step 3: Check for Prime Numbers
For each number in the range, check if it is divisible only by 1 and itself. If it is, then it is a prime number.
Step 4: Print the Prime Numbers
If a number is found to be prime, print it.
Step 5: End the Program
Once the loop is complete, the program will print all prime numbers within the range.
Python Code to Print Prime Numbers
Here’s the Python program to print prime numbers within the given range:
python
# Python program to print prime numbers within a given range# Get the lower and upper range from the user
low = int(input(“Enter lower range: “))
high = int(input(“Enter upper range: “))# Iterate through the given rangefor num inrange(low, high + 1):
if num > 1: # Check if the number is greater than 1for i inrange(2, num):
if num % i == 0: # If divisible by any number other than 1 and itself, it’s not primebreakelse:
print(num, end=” “) # If prime, print the number
Input:
go
Enter lower range: 3
Enter upper range: 15
Output:
3 5 7 11 13
Breaking Down the Python Code
1. Input Range:
The program first takes two inputs from the user: the lower range and the upper range. These values will define the scope within which we want to find the prime numbers.
2. For Loop to Check Numbers:
The for loop iterates through the numbers in the specified range, starting from the lower range up to the upper range.
3. Prime Number Check:
For each number, another for loop checks if it is divisible by any number other than 1 and itself. If it is, the number is not prime, and the loop breaks early. If the number is prime, it is printed.
4. Output Prime Numbers:
Finally, the program outputs all the prime numbers in the given range on a single line.
Why Learn How to Print Prime Numbers?
Understanding how to print prime numbers is more than just an exercise in coding. It helps improve your logic-building skills, which are essential for more complex programming tasks. Additionally, prime numbers have important applications in:
Cryptography: Prime numbers form the foundation of public key encryption systems.
Data Structures & Algorithms: Prime numbers are often used in hash functions.
Mathematical Problems: Prime numbers are used in various mathematical problem-solving techniques.
Conclusion
Printing prime numbers within a given range is a fundamental concept in programming. By understanding the logic behind prime number detection and implementing it in Python, you are not only learning a new programming technique but also laying the groundwork for more advanced coding concepts. Practice with different ranges and experiment with optimizing the code further.
Explore More with FACE Prep’s CRT Program
If you’re looking to improve your skills for campus recruitment and secure a job in the IT field, consider checking out FACE Prep’s Campus Recruitment Training (CRT) Program. Whether you’re a beginner or an intermediate coder, FACE Prep’s expert-led courses are designed to help you excel in coding, aptitude, reasoning, and interview preparation. Their comprehensive curriculum is tailored to boost your chances of success in campus placements and make you job-ready for the IT industry.