Author Topic: Turbo C program to find Twin Prime Numbers  (Read 4244 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Turbo C program to find Twin Prime Numbers
« on: November 28, 2011, 12:40:14 pm »
Here is the Turbo C source code to find and display twin prime numbers:

Code:
//Source Code Listing

#include <stdio.h>

int IsPrimeNumber(int num)
{
    int bPrime = 1;
    int factor = num / 2;
    int i = 0;
 
    for(i = 2; i <= factor; i++)
    {
      if( (num % i) == 0)
          bPrime = 0;
    }
    return bPrime;
}

void main()
{
   int i, firstprime = 0, secondprime = 0, totalTwinPrimeNumbers = 0;

   printf("Program to Print Twin Prime Numbers\n");

   for(int i = 2; i <= 1000; i++)
   {
      if(IsPrimeNumber(i) == 1)
      {
         if(firstprime == 0)
            firstprime = i;
         else 
            secondprime = i;
         int IsTwinPrimeNumber = ((secondprime - firstprime) == 2);
         if(IsTwinPrimeNumber == 1)
         {
            printf("(%d,%d)\n", firstprime, secondprime);
            totalTwinPrimeNumbers++;
         }
         secondprime = firstprime = i;
      }
   }
   printf("Total number of twin prime numbers under 1000: %d\n\n", totalTwinPrimeNumbers);
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1