This article let's try to find the all the prime numbers from 2 to 100, Given the range between two 200 so you have to find the prime numbers between these two ranges
Write a python program to find all the prime numbers from 2 to 100
Let's take a list variable and initialize it to empty list. we will be using this empty list to fill the prime numbers between 2 to 100
prime_list = []
Lets iterate all the numbers between 2 to 101 to get all the numbers from 2 to 100 using a for loop.
Use second for loop, iterate from 2 to number / 2. Using a statement try dividing a number by an index. if the number is completely divisible by index it is a prime number and breaks the for loop. using the for loop else part at the number 2 to prime list.
for number in range(2, 100+1):
for index in range(2, int(number/2)+1):
if number % index == 0:
break
else:
prime_list.append(number)
Once the for loop is completed the Prime Minister will have the list of prime numbers between 2 and hundred and finally print it using another for loop by iterating all the list items.
print("Prime Numbers from 2-100 are ...")
for item in prime_list:
print(item)
Final program:
prime_list = []
for number in range(2, 100+1):
for index in range(2, int(number/2)+1):
if number % index == 0:
break
else:
prime_list.append(number)
print("Prime Numbers from 2-100 are ...")
for item in prime_list:
print(item)
Output of the program is:
Prime Numbers from 2-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
Conclusion: try the program by modifying the range between 2 to 1000 run the program by yourself.
0 Comments