Home / C / c program to check for twin prime numbers

c program to check for twin prime numbers

Here is given c program for Twin Prime numbers.

But before let us see what exactly is a Twin Prime  number ?
A Twin Prime number are those numbers which are prime and having a difference of 2 between the two prime numbers.

Example: 11 and 13.
These numbers are itself prime numbers with a difference of 2 between them i.e d=13-11 which is 2 .Hence the two numbers are twin prime.

#include<stdio.h>
#include<conio.h>
#include<Math.h> // include a header file for abs functions for absolute value.
void main()
{
int i,n1,n2; // here n1 and n2 are the two input numbers
int d; // d is the variable to store the difference of two numbers
int c1=0,c2=0; // c1 and c2 are the counter values for checking the numbers are prime
clrscr();
printf("Enter a Number 1\n");
scanf("%d",&n1);
printf("Enter Number 2\n");
scanf("%d",&n2);
for(i=1;i<=n1;i++)
{
if(n1%i==0)
c1++;       // if counter c1 is equal to 2 then the number n1 is prime
}
for(i=1;i<=n2;i++)
{
if(n2%i==0)
c2++;        // if counter c2 is equal to 2 then the number n2  is prime
}
d=abs(n1-n2);     // here d is difference which calculate the absolute value i.e Modulous of |n1-n2|
if(d==2 && c1==2 && c2==2)
printf("The Two Numbers are Twin Prime\n");
else
printf("The two Numbers are Not Twin Prime\n");
getch();
}

 OUTPUT:

c program for twin prime
c program to check for twin prime numbers

Check Also

Restrict Mouse Pointer Position C Graphics Program

This C Program will restrict the Mouse Pointer location or to a particular position or x,y ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.