bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / tuition-c / APC-PRAC-041.c

APC-PRAC-041.c (1116B)


      1 /*
      2 Write a C program to find and print all twin prime pairs between 1 and n using nested loops.
      3 (Twin primes are prime numbers having a difference of 2, like 11 and 13)
      4 */
      5 /* Author: Amit Dutta, Date: 21-11-2025 */
      6 
      7 // This code has not been compiled.
      8 // If you find any issues, please create a new issue on GitHub regarding them.
      9 // Go to this link to create a new issue: https://github.com/notamitgamer/bsc/issues
     10 
     11 #include <stdio.h>
     12 #include <math.h>
     13 
     14 int isPrime(int n)
     15 {
     16     if (n < 2)
     17         return 0;
     18     if (n == 2)
     19         return 1;
     20     if (n % 2 == 0)
     21         return 0;
     22     int i, temp = (int)sqrt(n);
     23     for (i = 3; i <= temp; i += 2)
     24         if (n % i == 0)
     25             return 0;
     26     return 1;
     27 }
     28 
     29 int main()
     30 {
     31     int n, i, count = 0;
     32     printf("enter the n: ");
     33     scanf("%d", &n);
     34     printf("\nAll the twin numbers:  ");
     35     for (i = 1; i <= n - 2; i++)
     36     {
     37         if (isPrime(i))
     38         {
     39             if (isPrime(i + 2))
     40             {
     41                 printf("(%d, %d)  ", i, i + 2);
     42                 count++;
     43             }
     44         }
     45     }
     46     printf("\nCount; %d", count);
     47     return 0;
     48 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror