Thursday 10 August 2017

Write a C program to print prime pairs with distance <=3

Write  a program to print all possible pairs of prime numbers which are having distance 3 or less than 3  between them in a natural number line starting from 1 to given number N.
Example: prime pairs with distance <=3 – (2,3),  (2,5),  (3,5),  (5,7)
  (3,7), (5,11) are prime pairs with distance >3

Program:
#include<stdio.h>
main()
{
               int i,j,n,x=2,x1=3,x2=0,flag;
               printf("enter n value");
               scanf("%d",&n);
               printf("(%d,%d)\n",x,x1);
               for(i=4;i<=n;i++)
               {
                              flag=0;
                              for(j=2;j<i;j++)
                              {
                                             if(i%j == 0)
                                             {
                                                            flag=1;
                                                            break;
                                             }
                              }
                              if(flag == 0)
                              {
                                            
                                             x2=i;
                                             if(x2-x<=3)
                                                            printf("(%d,%d)\n",x,x2);
                                             if(x2-x1<=3)
                                                            printf("(%d,%d)\n",x1,x2);
                                             x=x1;
                                             x1=x2;
                              }
               }
}
              
Output:

No comments:

Post a Comment