Twin Prime Numbers in C
0 219
Twin prime numbers consist of prime number pairs separated by a gap of 2.
They are relatively rare but are fascinating for their proximity to each other in the number line.
Properties:
Twin primes are pairs of prime numbers.
The difference between each twin prime pair is always 2.
Both numbers in a twin prime pair are prime numbers.
Program 1:
// Program for Twin Prime Number in C #include<stdio.h>// Function to check if a number is prime int isPrime(int n) { if (n <= 1) { return 0; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return 0; // n is not prime if it has a divisor other than 1 and itself } } return 1; // n is prime } int main() { int start, end; printf("Enter the range to find twin prime numbers (start end): "); scanf("%d %d", &start, &end); printf("Twin prime numbers in c range %d to %d are:\n", start, end); for (int i = start; i <= end - 2; i++) { if (isPrime(i) && isPrime(i + 2)) { printf("(%d, %d)\n", i, i + 2); } } return 0; }
Output:
Enter the range to find twin prime numbers (start end): 1 100 Twin prime numbers in c range 1 to 100 are: (3, 5) (5, 7) (11, 13) (17, 19) (29, 31) (41, 43) (59, 61) (71, 73)
Program 2:
#include<stdio.h>// Function to check if a number is prime int isPrime(int n) { if (n <= 1) { return 0; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return 0; // n is not prime if it has a divisor other than 1 and itself } } return 1; // n is prime } int main() { int count = 0, n = 2, start, end; printf("Enter the digit of twin prime pairs to display: "); scanf("%d", &count); printf("First %d twin prime pairs are:\n", count); while (count > 0) { if (isPrime(n) && isPrime(n + 2)) { printf("(%d, %d)\n", n, n + 2); count--; } n++; } return 0; }
Output:
Enter the digit of twin prime pairs to display: 2 First 2 twin prime pairs are: (3, 5) (5, 7)
Both programs utilize the isPrime function to check if a number is prime.
Program 1 takes a range from the user and finds twin primes within that range.
Program 2 takes a count from the user and displays the specified number of twin prime pairs starting from the smallest twin prime.
Share:
Comments
Waiting for your comments