Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Find Prime Numbers in Range
Welcome to this captivating tutorial on Python program to print all prime numbers in a given range or interval!
Prime numbers hold a special place in mathematics and have intrigued mathematicians and number enthusiasts for centuries. These unique numbers possess an inherent beauty and are divisible only by 1 and themselves, leaving no room for any other divisors in between.
Examples of prime numbers include 2, 3, 5, 7, 11, and so on.
But why should we care about prime numbers in real life? The concept of prime numbers finds application in various domains, ranging from cryptography to number theory and algorithm design.
They are the fundamental building blocks behind encryption algorithms that safeguard our digital communication, ensuring secure transactions and confidential information exchange.
Consider online shopping, where prime numbers play a crucial role in secure communication protocols, keeping your credit card information safe from prying eyes. Additionally, prime numbers find application in fields such as computer science, data analysis, and prime factorization algorithms.
Here, we will write a Python program to find prime numbers in range. We'll explore various techniques, including optimized algorithms and practical coding examples, to efficiently generate and validate prime numbers.
You will also use prime number program in Python to print 1 to 100. So, let’s get started!
Python Program to Find Prime Numbers in Range
Code
# Print all prime numbers in a given range in Python
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Taking input from the user
start = int(input("Enter the starting number of the range: "))
end = int(input("Enter the ending number of the range: "))
# Printing prime numbers within the range
print("Prime numbers between", start, "and", end, "are:")
for num in range(start, end+1):
if is_prime(num):
print(num)
Output
Enter the starting number of the range: 1
Enter the ending number of the range: 50
Prime numbers between 1 and 50 are:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
Explanation
-
We used the is_prime function that takes a number n as input and determines if it is a prime number. The function first checks if n is less than 2 because prime numbers are defined as greater than 1.
-
Then, using a for loop, it iterates from 2 to the square root of n (converted to an integer plus 1). Within each iteration, it checks if n is divisible by the current value of i.
-
If it is divisible, n is not a prime number and the function returns False. If no divisors are found, the function returns True, indicating that n is a prime number.
-
We then take input from the user to specify the starting and ending numbers of the range within which we want to find the prime numbers.
-
Next, we use a for loop to iterate through each number within the given range. For each number, we call the is_prime function to check if it is prime. If it is, we print the number.
Print Prime Numbers from 1 to 100 in Python
Code
# Prime number program in Python to print 1 to 100
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Printing prime numbers from 1 to 100
print("Prime numbers from 1 to 100 are:")
for num in range(1, 101):
if is_prime(num):
print(num)
Output
Prime numbers from 1 to 100 are:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Explanation
In this program, we define a function called is_prime that takes a number n as input and determines if it is a prime number. The function follows the same logic as explained in the previous example.
We then use a for loop to iterate through each number from 1 to 100. For each number, we call the is_prime function to check if it is prime. If it is, we print the number.
By running this program, you will see all the prime numbers from 1 to 100 being printed.
Print First n Prime Numbers in Python
Code
# Python program to print first n prime numbers
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# Taking input from the user
n = int(input("Enter the value of n: "))
# Printing the first n prime numbers
count = 0
num = 2
print("The first", n, "prime numbers are:")
while count < n:
if is_prime(num):
print(num)
count += 1
num += 1
Output
Enter the value of n: 10
The first 10 prime numbers are:
2
3
5
7
11
13
17
19
23
29
Explanation
-
Here, we defined the is_prime function that takes a number num as input and checks if it is a prime number. The function follows the same logic as in the previous examples.
-
We take input from the user to specify the value of n, which represents the number of prime numbers to be printed.
-
Using a while loop, we keep track of the count of prime numbers found (count) and iterate through numbers starting from 2 (num). Inside the loop, if num is prime, we print it, increment the count, and proceed to the next number. We continue this process until we find first n prime numbers.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program