APC-PRAC-041.c (1307B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 12 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* 9 Write a C program to find and print all twin prime pairs between 1 and n using nested loops. 10 (Twin primes are prime numbers having a difference of 2, like 11 and 13) 11 */ 12 /* Author: Amit Dutta, Date: 21-11-2025 */ 13 14 // This code has not been compiled. 15 // If you find any issues, please create a new issue on GitHub regarding them. 16 // Go to this link to create a new issue: https://github.com/notamitgamer/bsc/issues 17 18 #include <stdio.h> 19 #include <math.h> 20 21 int isPrime(int n) 22 { 23 if (n < 2) 24 return 0; 25 if (n == 2) 26 return 1; 27 if (n % 2 == 0) 28 return 0; 29 int i, temp = (int)sqrt(n); 30 for (i = 3; i <= temp; i += 2) 31 if (n % i == 0) 32 return 0; 33 return 1; 34 } 35 36 int main() 37 { 38 int n, i, count = 0; 39 printf("enter the n: "); 40 scanf("%d", &n); 41 printf("\nAll the twin numbers: "); 42 for (i = 1; i <= n - 2; i++) 43 { 44 if (isPrime(i)) 45 { 46 if (isPrime(i + 2)) 47 { 48 printf("(%d, %d) ", i, i + 2); 49 count++; 50 } 51 } 52 } 53 printf("\nCount; %d", count); 54 return 0; 55 }