Print all pairs of two digit twin prime numbers using for loop in Python
for Loop - Question 11
In this question, we will see how to print all pairs of two-digit twin-prime numbers in Python programming using for loop. To know more about for loop click on the for loop lesson.
Q11) Write a program in Python to print all pairs of two-digit twin-prime numbers using for loop.
A twin-prime numbers are numbers that differs from another prime number by two. Examples of two-digit twin-prime numbers are: (11, 13), (17, 19), (29, 31), (41, 43), etc.
Program
print('List of all twin prime numbers between 10 to 99');
for i in range(10,100):
fc=0
for j in range(1,i+1):
if i%j==0:
fc=fc+1 # counting the factors of i (value of i)
if fc==2:
fc=0
n=i+2 # taking another number from the current value of i with a difference of 2
for j in range(1,n+1):
if n%j==0:
fc=fc+1 # counting the factors of n (value of n)
if fc==2:
print('(%d, %d)' %(i,n))
Output
List of all twin prime numbers between 10 to 99 (11, 13) (17, 19) (29, 31) (41, 43) (59, 61) (71, 73)