Twin Prime Numbers In C

For instance, the pair (59, 61) and (71, 73) represent twin prime pairs where the gap between the numbers is 2, such as 61 - 59 = 2 and 73 - 71 = 2.

Properties of the twin prime number

  • (2, 3) are not considered twin primes because there is no composite number between them, and neither is the difference of two prime numbers (2, 3) is equal to 2.
  • (3, 5) is a pair of twin primes because the difference of two primes (5 - 3 = 2) is 2 and hence (3, 5) is a twin prime.
  • Every twin other than (3, 5) is in the form of a prime number (6n - 1, 6n + 1), where n defines the natural number.
  • 5 is the only prime number in which the difference between both positive and negative primes is 5. So, 5 appears in two prime pairs (3, 5) and (5, 7).
  • If we add two pairs of twin prime numbers, the result of twin primes will be divisible by 12, except for (3, 5) pairs of twin prime numbers.
  • Program to print twin primes from 2 to n using user defined function

Develop a C program that can identify twin prime numbers within the range of 2 to n.

Program1.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// declare variables
	int i, num, count = 0;
	printf (" Enter the last number: ");
	scanf (" %d", &num); // get the last number
	
	for (i = 2; i <= num; i++)
	{
		if (twinprime (i) && twinprime (i+2))
		{
			printf (" \n The twin prime number is: (%d, %d) ", i, i+2);
			count++; // counter increment by 1
		}
	}
	printf (" \n \n Total number of twin prime pairs: %d", count);
	return 0;
}
// function definition 
int twinprime( int n)
{
	int i = 2;
	// use for loop to find the twin prime
	for (i = 2; i <= n/2; i++)
	{
		// if n is completely divisible by 1 without leaving any remainder, it returns 0
		if (n%i == 0)
			return 0;
	}
	// otherwise it returns 1
	if (i > n / 2)
		return 1;
}

Output

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 provided code, a for loop is utilized to iterate through numbers within the defined range of 2 to 100. Subsequently, a custom function named twinprime is defined and invoked within the loop to verify the primality of the 'i' and 'i+2' elements. Upon meeting the specified criteria, it displays the count of twin prime pairs along with their respective numbers.

Program to print twin primes using the Sieve of Eratosthenes algorithm

The Sieve of Eratosthenes method is utilized to generate a roster of prime numbers that are equal to or below a specified number (n). This technique involves progressing through consecutive numbers up to n, verifying if both the i-th and (i + 2) numbers are prime, and subsequently displaying the prime number. If the conditions are not met, the algorithm proceeds to the subsequent number in search of twin primes. For instance, (3, 5), (5, 7), and (11, 13) exemplify the application of the Sieve of Eratosthenes algorithm.

Let's explore an illustration of finding twin prime numbers using the Sieve of Eratosthenes method.

Program2.c

Example

#include <stdio.h>
#include <string.h>
int main()
{
	// declare an integer variable
	int num;
	printf (" Enter the last number to get twin prime numbers: ");
	scanf (" %d", &num); 	
	// call twinprime() function
	twinprime( num); // pass num as an argument
	return 0;
}
void twinprime (int n)
{
	int j;
	/* here we create a unsigned char array 'prime[]' and initialize all its entries as true. */
	unsigned char prime[n + 1]; // declaration of the unsigned char prime[] array	
	memset (prime, 1, sizeof(prime));	
	// use for loop to iterate over a certain range
	for ( j = 2; (j * j) <= n; j++)
	{
		// if the condition of prime[j] remains true, it is a prime
		if (prime [j] == 1)
		{
			int i = 0;
			// update the multiples of j
			for (i = (j * 2); i <= n; i += j)	
				prime[i] = 0;
		}
	  } 	  
	  // now check for twin prime numbers and print the twin primes	  
	  for ( j = 2; j <= (n - 2); j++)
	  {
	  	if (prime[j] && prime[j + 2])
	  		printf (" \n The twin prime numbers: (%d, %d) ", j, j +2);	   	
	   } 
}

Output

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 not

Let's explore an illustration of determining whether a specified number is a twin prime or not within the C programming language.

Program3.c

Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// function definition of the checktwin()
void checktwin(num1, num2)
{
	// declare integer variables
	int i;	
	// declare and initialize isprime
	unsigned char isprime = 1;	
	// use for loop to check the prime number for the first number 
	for ( i = 2; i <= (num1 / 2); i++ )
	{
		// if num1 is completely divisible by i, the if block is executed
		if (num1 % i == 0)
		{
			isprime = 0;
			break;
		}			
	}
	/* here isprime is called and abs() function get the absolute result of num1 and num2 */
	if (isprime && abs(num1 - num2) == 2)
	{
		// declare isprime2 as unsigned char and initialize as 1 that represents true.
		unsigned char isprime2 = 1;		
		// use for loop to check the second number is a prime number
		for ( i = 2; i <= num2 / 2; i++)
		{
			// it checks num2 is completely divisible by i and return 0
			if (num2 % i == 0)
			{
				// isprime2 initialize by 0
				isprime2 = 0;
				break;
			}
		}
		// check the isprime2 condition
		if (isprime2)
		{
			printf (" %d and %d are twin prime", num1, num2);
		}
		else
			printf (" %d and %d are not twin prime", num1, num2);	
	}
	else
		printf (" %d and %d are not twin prime number", num1, num2);
}
int main()
{
	int num1, num2;	
	printf (" Enter the first number: ");
	scanf ("%d", &num1); // get first number	
	printf (" Enter the second number: ");
	scanf (" %d", &num2); // get second number	
	checktwin(num1, num2); /* call the checktwin() function and pass num1 and num2 as parameters. */
	return 0;
}

Output

Output

Enter the first number: 71
 Enter the second number: 73
 71 and 73 are twin prime

2nd execution:

Example

Enter the first number: 15
 Enter the second number: 17
 15 and 17 are not twin prime

In the program mentioned above, we request two numerical inputs from the user to determine if the provided numbers are twin prime. In this scenario, 5 and 7 are considered prime numbers, and the function confirms that they are twin primes. Likewise, when we input 15 and 17, the function identifies the numbers as not being twin primes.

Input Required

This code uses input(). Please provide values below: