C Program to display Twin Primes

This C Program to display Twin Primes – Difference between 2 prime numbers is 2 is called “Twin Primes”, for example (17 , 19) are twin primes.


#include <stdio.h>
void main()
{
int n,i,k,r,ary[50],p;
printf(“Enter Limit : “);
scanf(“%d”,&r);
i=1;
p=0;
while(i<=r)
{
k=0;n=1;
while(n<=i)
{
if(i%n==0)
k++;
n++;
}
if(k==2)
{
ary[p]=i;
p++;
}
i++;
}
for(n=0;n<p;n++)
{
if(ary[n+1]-ary[n]==2)
printf(“\n%d & %d are TWIN PRIMEs \n”,ary[n],ary[n+1]);
}
}

Share this