In this case study, we will discuss a Python program designed to find all prime numbers within a specified range. The program utilizes a nested loop structure to efficiently identify prime numbers.
Given a range of numbers, we aim to identify and list all prime numbers within that range.
To solve this problem, we employ a nested loop structure:
start = 10
end = 20
primeNumbers = []
for num in range(start, end + 1):
for i in range(2, num):
if num % i == 0:
break
else:
primeNumbers.append(num)
print("Prime numbers between", start, "and", end, "are:", primeNumbers)
Prime numbers between 10 and 20 are: [11, 13, 17, 19]
break
.else
block associated with the inner loop is executed, adding the number to the list of prime numbers.else
block of the inner loop is only executed if the loop completes all iterations without encountering a break
statement, i.e., if the number is not divisible by any number other than 1 and itself.The provided Python program efficiently identifies prime numbers within a specified range using a nested loop structure. It demonstrates a practical application of nested loops and conditional statements in solving mathematical problems.