Computer Science, asked by BarbieDGirl, 7 months ago

Twin primes are consecutive prime numbers whose difference is 2. for example (3,5) , (11,13) ,(17,19) are all twin primes . we define the distance of any twin prime pair from a positive integer as follows if (p1, p2) is a twin prime pair and n is a positive integer then the distance of the twin prime pair from n is :
minimum (abs(n-p1),abs return the whole value of it's argument and minimum returns the smaller of it's two argument.
WAP that reads in appositive integer n and prints out the twin prime pair that has the least distance from n. for example if n is 30 the pair is (29,31) ,if n is 13 it is (11,13), if n is 49 it is (41,43) if n is 54 it is (59,61)
sample data
inputs :give the number :34
outputs : number read in is :34 P1= 29 P2=31
input: give the number :60 P1=59 P2=61
PLEASE GIVE THE WHOLE PROGRAM I'LL MARK YOU AS BRAINLIST NO SPAM!!​

Answers

Answered by fadilwaqaar1
2

Explanation:

#include<iostream.h>

#include<conio.h>

void main()

{

  clrscr();

  int i,j,k=30000;

  int n,p2,p1,l;

  int minimum(int,int);

  int prime(int);

  int abs(int);

  cout<<"Enter a number : ";

  cin>>n;

  for(i=0;i<2*n;i++)

  {

    p1=prime(i);

    p2=prime(i+2);

    if (p1==0 || p2==0)

    {

      continue;

    }

    if(minimum(abs(n-p1),abs(n-p2))<k)

    {

      k=minimum(abs(n-p1),abs(n-p2));

      j=p1;l=p2;

    }

  }

  cout<<"\n twin prime nearest to "<<n<<"="<<j<<","<<l;

  getch();

}

int abs(int n)

{

  if(n<0)

    return(-n);

  else

    return(n);

}

int minimum(int a,int b)

{

  int n=(a<b)?a:b;

  return(n);

}

int prime(int n)

{

  int f=n;

  for(int i=2;i<n-1;i++)

  {

    if(n%i==0)

    {

      f=0;

      break;

    }

  }

  return(f);

} public class ISC02Q1

{

  private int prime(int n)

  {

    int f=n;

    for(int i=2;i<n-1;i++)

    {

      if(n%i==0)

      {

        f=0;

        break;

      }

    }

    return(f);

  }

  private int abs(int n)

  {

    if(n<0)

      return(-n);

    else

      return(n);

  }

  private int minimum(int a,int b)

  {

    int n=(a<b)?a:b;

    return(n);

  }

  public void main(int n)

  {

    int i,j=0,k=30000;

    int p2=0,p1=0,l=0;

    for(i=0;i<2*n;i++)

    {

      p1=prime(i);

      p2=prime(i+2);

      if(p1==0 || p2==0)

      {

        continue;

      }

      if(minimum(abs(n-p1),abs(n-p2))<k)

      {

        k=minimum(abs(n-p1),abs(n-p2));

        j=p1;

        l=p2;

      }

    }

    System.out.println("Twin prime nearest to "

+n+"="+j+","+l);

  }

please mark my answer as brainliest

Similar questions