Twin Prime Numbers in CThis topic will discuss twin primes and how we can print twin primes in the C programming language. A twin prime is a pair of prime numbers representing the difference of two prime numbers always 2 and is therefore called a twin prime. In simple words, if the difference between two prime numbers is 2 and there is exactly one composite number between them, then the number is called twin prime numbers. For example, (59, 61) and (71, 73) are twin prime pairs, and the difference between these numbers is 61 - 59 = 2, and 73 - 71 = 2. Properties of the twin prime number
Program to print twin primes from 2 to n using user defined functionCreate a program in C programming language to generate twin prime number between 2 to n. Program1.c Output Enter the last number: 100 The twin prime number is: (3, 5) The twin prime number is: (5, 7) The twin prime number is: (11, 13) The twin prime number is: (17, 19) The twin prime number is: (29, 31) The twin prime number is: (41, 43) The twin prime number is: (59, 61) The twin prime number is: (71, 73) Total number of twin prime pairs: 8 In the above program, a for loop that iterates over the numbers between the specified range of 2 to 100. After that, we create a twinprime() as a user-defined function called inside the for loop to check the primes of the 'i' and 'i+2' index. And if the condition is met, it prints the total pair of twin prime and the twin prime numbers. Program to print twin primes using the Sieve of Eratosthenes algorithmThe Sieve of Eratosthenes is an approach to get a list of all prime numbers that are less than or equal to a given number (n). It iterates the prime number up to n consecutive number and checks that both the ith and (i + 2) numbers are prime, and then it prints the prime number; otherwise, it moves to the next number to get the twin primes. Here, (3, 5), (5, 7), (11, 13) are examples of the Sieve of Eratosthenes algorithm. Let's consider an example to get the twin primes using the Sieve of Eratosthenes algorithm. Program2.c Output Enter the last number to get twin prime numbers: 200 The twin prime numbers: (3, 5) The twin prime numbers: (5, 7) The twin prime numbers: (11, 13) The twin prime numbers: (17, 19) The twin prime numbers: (29, 31) The twin prime numbers: (41, 43) The twin prime numbers: (59, 61) The twin prime numbers: (71, 73) The twin prime numbers: (101, 103) The twin prime numbers: (107, 109) The twin prime numbers: (137, 139) The twin prime numbers: (149, 151) The twin prime numbers: (179, 181) The twin prime numbers: (191, 193) The twin prime numbers: (197, 199) Program to check whether a given number is a twin prime or notLet us consider an example of whether a given number is twin prime or not in the C programming language. Program3.c Output Enter the first number: 71 Enter the second number: 73 71 and 73 are twin prime 2nd execution: Enter the first number: 15 Enter the second number: 17 15 and 17 are not twin prime In the above program, we take two numbers from the user to check whether the given number is twin prime or not. Here we take 5 and 7 as the prime number, and the function returns 5 and 7 are twin prime. Similarly, we take the numbers 15 and 17, and the function returns numbers, not twin primes. Next Topicstrchr() function in C |